English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

PHP strftime() Function Usage and Examples

PHP Date & Time Function Manual

The strftime() function formats the local time/date according to the region setting

Definition and usage

strftimeThe function accepts a format string as a parameter and formats the local date and time according to the region setting/time..

Syntax

strftime($format [, $timestamp])

Returns the string after formatting the given timestamp with the given format string. If no timestamp is provided, the current local time is used. The notation of month, day of the week, and other language-related strings are related to the current locale set by setlocale().

Parameter

Serial numberParameters and descriptions
1

timestamp(必需)

This is an integer value representing the Unix timestamp of the specified current time value.

2

format(可选)

This is a string value indicating that you need to format the date/the format of time. Specify how to return the result:

  • %a - the abbreviation of the day of the week

  • %A - the full name of the day of the week

  • %b - the abbreviation of the month name

  • %B - the full name of the month

  • %c - the preferred date and time notation

  • %C - the number representing the century (the year divided by 100, ranging from 00 to 99)

  • %d - the day of the month (01 to 31)

  • %D - time format, which is the same as %m/%d/which is the same as the %y notation

  • %e - the day of the month (1 to 31)

  • %g - similar to the %G notation but without the century

  • %G - corresponding to the ISO week number 4 the number of digits in the year (see %V)

  • %h - which is the same as the %b notation

  • %H - hour, using 24 12-hour format (00 to 23)

  • %I - hour, using 12 12-hour format (01 to 12)

  • %j - the day of the year (001 to 366)

  • %m - month (01 to 12)

  • %M - minute

  • %n - newline

  • %p - am or pm corresponding to the given time value

  • %r - The time notation in a.m. and p.m. format

  • %R - 24 The time notation in 12-hour format

  • %S - second

  • %t - tab tab

  • %T -

  • 㩵n - The numeric representation of the day of the week (1 to 7),Monday[星期一] = 1. Warning: In the Sun Solaris system, Sunday[星期日] = 1

  • %U - The number of weeks contained in the year, starting from the first Sunday as the first day of the first week

  • %V - the ISO weeks contained in the year 8601 the week number under the format (01 to 53),week 1 Indicates the first week of the year, which must have at least four days and Monday as the first day of the week

  • %W - The number of weeks contained in the year, starting from the first Monday as the first day of the first week

  • %w - Represents a day of the week as a decimal number, Sunday[星期日] = 0

  • %x - Preferred date representation without time

  • %X - Preferred time representation without date

  • %y - Year representation that does not include a number representing the century (range from 00 to 99)

  • %Y - Year representation that includes a number representing the century

  • %Z or %z - Time zone name or abbreviation

  • %% - Outputs a % character

Return Value

PHP strftime()This function returns a string value representing the formatted time. You can usesetlocale()method changes the names of months and days of the week to other languages.

PHP version

This function was originally introduced in PHP version5.3introduced in and can be used in all higher versions.

Online Example

The following examples demonstratestrftime()Usage of the function-

<?php
   $date = strftime("%A %d %B %G");
   $time = strftime("%T");
   print("Date: ".$date."\n");
   print("Time: ".$time);
?>
Test and see‹/›

Output Result

Date: Wednesday 13 May 2020
Time: 06:28:07

Online Example

Let's try this function by passing the timestamp parameter (and format)-

<?php
   $timestamp = mktime(7, 36, 45, 06, 25, 2017); 
   $date = strftime("%A %d %B %G %T", $timestamp );
   print("Date: ".$date."\n");
?>
Test and see‹/›

Output Result

Date: Sunday 25 June 2017 07:36:45

Online Example

The following example prints the specific date's day of the week and month in Catalan-

<?php
   setlocale(LC_TIME, "ca_ES", "Catalan_Spain", "Catalan");
   $date = strftime("%A %d %B %G %T");
   print("Date: ".$date."\n");
?>
Test and see‹/›

Output Result

Date: Wednesday 13 maig 2020 08:14:19

Online Example

<?php
   setlocale(LC_TIME, "en_US");   
   echo strftime("%b %d %Y %H:%M:%S", mktime(20, 0, 0, 12, 31, 2015)) . "\n";
   echo gmstrftime("%b %d %Y %H:%M:%S", mktime(20, 0, 0, 12, 31, 2015)) . "\n"; 
?>
Test and see‹/›

This produces the following result-

Dec 31 2015 20:00:00
Dec 31 2015 20:00:00