How to use strftime function in PHP

Source: Internet
Author: User
This article mainly introduces the PHP strftime function of the detailed usage, strftime () function according to locale format local time/date, usually can be used for time format, the need for friends can refer to the next

Definition and usage

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

Grammar

Strftime (format,timestamp) parameter description
Format is optional. Specifies how the results are returned.
Timestamp is optional.
Hints and Notes
Tip: The same behavior as Gmstrftime (), the difference is that the return time is local time.

Example

Results of output strftime () and Gmstrftime ():

<?php Tutorial Echo (strftime ("%b%d%Y%x", Mktime (20,0,0,12,31,98))), Echo (Gmstrftime ("%b%d%Y%x", Mktime (20,0,0,12,31,98 ));//Output The current date, time, and time zone echo ("It is%a on%b%d,%Y,%x gmstrftime zone:%Z", Times ());? >

The strftime () function converts date strings in YYYY-MM-DD HH:MM:SS format to other forms of strings.
The syntax of Strftime () is strftime (format, date/time, modifier, modifier, ...)

It can be formatted with the following symbols for dates and times:

%a Abbreviation of the current day of the week Sun to Sat
%A Full name of the current day of the week Sunday to Saturday
%d Day of the Month, decimal number (range from 01 to 31) 01 to 31
%e The day of the month, the decimal number, a digit before the number is preceded by a space (ranging from ' 1 ' to ' 31 ') that has not been implemented on Windows as described. See below for more information. 1 to 31
%j Day of the year, three-bit decimal number with leading zeros (range from 001 to 366) 001 to 366
%u A decimal number that conforms to ISO-8601 day of the week [1,7],1 represents Monday 1 (Monday) to 7 (Sunday)
%w Day of the week, Sunday is 0 0 (Sunday) to 6 (Saturday)
Week --- ---
%u Week of the year, starting from the first Sunday of the first week as the first day (for the 13th full week of the year)
%V %V-The iso-8601:1988 format for the week ordinal of the year, ranging from 01 to 53, the 1th week is the first of the year with at least 4 days, Monday as the first day of the week. (%G or%G as the number of weeks for the specified timestamp.) ) 01 to accounts (where overlapping week)
%W Number of weeks of the year, starting from the first Monday of the first week as the first day Week (for the 46th, beginning with a Monday)
Month --- ---
%b Shorthand for the current region month Jan to Dec
%B Full name of the current region month January to December
%h Shorthand for the current region month (alias for%b) Jan to Dec
%m Two-digit month 01 (is January) to 12 (is December)
Years --- ---
%c Two-digit display century (year divided by 100, truncated into integers) 19 is 20th century
%g 2-digit year, which conforms to iso-8601:1988 weeks (see%V). The same as the%V format and value, except if the ISO week number belongs to the previous year or the following year, then the year is used. For example: January 6, 2009 that week is 09.
%G Full four-digit version of%g For example: January 3, 2009 that week is 2008.
%y Two-digit display of the year For example: 09 is 2009,79 is 1979
%Y Four-digit year of display For example: 2038
Time --- ---
%H Display two-bit hours in 24-hour format 00 to 23
%I Display two-bit hours in 12-hour format 01 to 12
%l (lowercase of ' l ') Displays hours in 12-hour format with spaces in front of a single digit 1 to 12
%M Two-bit number of minutes 00 to 59
%p Uppercase "AM" or "PM" for the specified time For example: 00:31 is am, 22:23 is PM
%P lowercase "AM" or "PM" for the specified time For example: 00:31 is am, 22:23 is PM
%r Same as "%i:%m:%s%p." For example: 21:34:17 is 09:34:17 PM
%R Same as "%h:%m." For example: 12:35 am is 00:35,4:44 PM is 16:44
%s Two-digit number indicates seconds 00 to 59
%T Same as "%h:%m:%s." For example: 09:34:17 PM is 21:34:17
%x Preferred time notation for the current region, excluding dates Example: 03:59:16 or 15:59:16
%z Time zone offset or shorthand from UTC (determined by the operating system) For example: Eastern time is-0500 or EST
%Z %z No time zone offset or shorthand given for UTC (determined by the operating system) For example:-0500 or EST is Eastern time
Time and date stamp --- ---
%c Preferred date-time expression for the current region For example: February 5, 2009 12:45:10 is Tue 5 00:45:10 2009
%d Same as "%m/%d/%y." For example: February 5, 2009 is 02/05/09
%F Same as "%y-%m-%d" (commonly used in database datestamps) For example: February 5, 2009 is 2009-02-05
%s Timestamp for the Unix era (as with the time () function) For example: September 10, 1979 8:40 A.M. 00 seconds is 305815200
%x Preferred time notation for the current region, excluding time For example: February 5, 2009 is 02/05/09
Other --- ---
%n Line break ("\ n") ---
%t tab character ("\ T") ---
%% Percent character on text ("%")

The maximum length of this parameter is 1023 characters.

Warning
Although ISO 9,889:1999 (the current C standard) clearly states that the week starts in Monday, the Sun Solaris week seems to start in Sunday and as 1. Therefore, the results of%u may not be the same as described in the manual.

Warning
Windows only: The%e modifier modifier in this function does not yet support windows. In order to get this value can be replaced by the% #d modifier. The following example shows how to write a cross-platform supported function.

Warning
For Mac OS X only: the%P modifier in this function does not yet support Mac OS X.

Timestamp
The optional timestamp parameter is an integer Unix timestamp, if unspecified, the parameter value defaults to the current local time. In other words, its value defaults to the return value of time ().

An example of the use of strftime () is as follows:

Select Strftime ('%y.%m.%d%h:%m:%s ', ' Now ', ' localtime ');
Result: 2018.06.21 21:41:09
In fact, the better usage is this, such as the system, this month or this year's expenditure:
Select Strftime ('%y-%m ', date) as month, sum (expense) as monthly expenditure from the Flow Account group by month;

Example #1 strftime () locale examples

<?phpsetlocale (Lc_time, "C"); Echo strftime ("%A"); SetLocale (Lc_time, "Fi_fi"); Echo strftime ("In Finnish is%A,"); SetLocale (Lc_time, "fr_fr"), Echo strftime ("in French%A and"), setlocale (Lc_time, "De_de"); Echo strftime ("in German%a.\ n ");? >

Example #2 ISO 8601:1988 Week number Example

<?php/* December 2002/january 2003ISOWk M Tu W Thu F Sa Su---------------------------------51 16 17 18 19 20 2252-291 1 2 3 4-6 7 8 9-All-in-one 123-*///OUTPUTS:12/2 8/2002-%v,%g,%y = 52,2002,2002echo "12/28/2002-%v,%g,%y =". Strftime ("%v,%g,%y", Strtotime ("12/28/2002")). "\ n";//outputs:12/30/2002-%v,%g,%y = 1,2003,2002echo "12/30/2002-%v,%g,%y =". Strftime ("%v,%g,%y", Strtotime ("12/30/2002")). "\ n";//outputs:1/3/2003-%v,%g,%y = 1,2003,2003echo "1/3/2003-%v,%g,%y =". Strftime ("%v,%g,%y", Strtotime ("1/3/2003")). "\ n";//outputs:1/10/2003-%v,%g,%y = 2,2003,2003echo "1/10/2003-%v,%g,%y =". Strftime ("%v,%g,%y", Strtotime ("1/10/2003")). "\ n";/* December 2004/january 2005ISOWk M Tu W Thu F Sa Su---------------------------------51 13 14 15 16 17 18 1952-2653 1 3 4 5 6 7 8 Ten-to-one, and the *///outputs:12/23/ 2004-%v,%g,%y = 52,2004,2004echo "12/23/2004-%v,%g,%y =". Strftime ("%v,%g,%y", Strtotime ("12/23/2004")). "\ n";//outputs:12/31/2004-%v,%g,%y = 53,2004,2004echo "12/31/2004-%v,%g,%y =". Strftime ("%v,%g,%y", Strtotime ("12/31/2004")). "\ n";//outputs:1/2/2005-%v,%g,%y = 53,2004,2005echo "1/2/2005-%v,%g,%y =". Strftime ("%v,%g,%y", Strtotime ("1/2/2005")). "\ n";//outputs:1/3/2005-%v,%g,%y = 1,2005,2005echo "1/3/2005-%v,%g,%y =". Strftime ("%v,%g,%y", Strtotime ("1/3/2005")). "\ n";? >

Example #3 Cross platform compatible Example using the%e modifier

<?php//Jan 1:results in: '%e%1% ' (%, E, percent,%e,%) $format = '%%e%%%e%% ';//Check for Windows to find and replace th e%e//Modifier correctlyif (Strtoupper (substr (php_os, 0, 3)) = = ' WIN ') {  $format = preg_replace (' # (? <!%) ((?:%) *)%e# ', ' \1% #d ', $format);} echo strftime ($format);? >

Example #4 Display All known and unknown formats.

<?php//Describe the formats. $strftimeFormats = Array (' a ' = = ' A full textual representation of the day ', ' B ' =&gt ; ' Full month name, based on the locale ', ' C ' + ' digit representation of the century (year pided by, truncated To an integer) ', ' D ' = ' = ' same as '%m/%d/%y ', ' E ' = ', ' F ' = ' same ' as '%y-%m-%d ' ', ' G ' = = ' The full fou R-digit version of%g ', ' H ' = ' and ' II digit representation of the hour in 24-hour format ', ' I ' + = ' digit Represe Ntation of the hour in 12-hour format ', ' J ' + = ', ' K ' = = ', ' L ' + ', ' M ' = ' and '-digit representation   Of the minute ', ' N ' = ', ' O ' = ', ' P ' = ' lower-case ' am ' or ' PM ' based on the given time ', ' Q ' = = ', ' R ' = ' same as '%h:%m ', ' S ' and ' digit representation of the second ', ' T ' and ' same as '%h:%m:%s ', ' U ' = ' Week number of the given year, starting with the first Sunday as the first Week ', ' V ' = ' iso-8601:1988 Week nu Mber of the given year, starting with the first week of the year with at least 4 weekdays, with Monday being the start of the week ', ' W ' = ' A numeric representation of the week of the year, starting with the first Monday as the first week ', ' X ' = = ' Preferre D time representation based on locale, without the date ', ' Y ' = ' four digit representation for the year ', ' Z ' = = ' The time zone offset/abbreviation option not given by%z (depends on operating system) ', ' a ' = ' an abbreviated TEXTU Al representation of the day ', ' B ' + = ' abbreviated month name, based on the locale ', ' c ' = ' Preferred date and Ti  Me stamp based on local ', ' d ' = ' = ' Two-digit Day of the month (with leading zeros) ', ' e ' + = ' Day of the month, with A space preceding single digits ', ' f ' + = ', ' g ' = ' and '-digit representation of the year going by iso-8601:1988 Standards (see%V) ', ' h ' = = ' abbreviated month name, based on the locale (an alias of%b) ', ' i ' + = ', ' j ' + = ' Day of the year, 3 digiTS with leading zeros ', ' k ' = = ' Hour in 24-hour format, with a space preceding single digits ', ' l ' + ' Hour in 12- hour format, with a space preceding single digits ', ' m ' = ' digit representation of the month ', ' n ' = ' a newl ine character ("\ n") ', ' o ' = = ', ' p ' = ' upper-case ' AM ' or ' PM ' based on the given time ', ' q ' = ', ' R ' =& Gt  ' Same as '%i:%m:%s%p ' ', ' S ' and ' Unix Epoch time timestamp ', ' t ' = ' A tab character ("\ T") ', ' u ' = ' ISO-8601 ' Numeric representation of the day of the "The Week ', ' v ' = =", ' w ' = ' numeric representation of the day of the week ' , ' x ' = ' Preferred date representation based on locale, without the time ', ' y ' ~ = ', ' digit representation of th E year ', ' z ' = = ' Either the time zone offset from UTC or the abbreviation (depends on operating system) ', '% ' = = ' A literal percentage character ("%") ', '//Results. $strftimeValues = Array ();//Evaluate The formats whilst suppressing an Y Errors.foreach ($stRftimeformats as $format = = $description) {if (False!== ($value = @strftime ("%{$format}")) {$strftimeValues [$form  at] = $value; }}//Find the longest value. $maxValueLength = 2 + max (Array_map (' strlen ', $strftimeValues));//Report known FORMATS.FOREAC H ($strftimeValues as $format + $value) {echo "known format: ' {$format} ' =", Str_pad ("' {$value} '", $maxValueLength) , "({$strftimeFormats [$format]}) \ n";}  Report Unknown Formats.foreach (Array_diff_key ($strftimeFormats, $strftimeValues) as $format = = $description) { echo "Unknown format: ' {$format} '", Str_pad (', $maxValueLength), ($description? ") ({$description}) ":", "\ n";}? >

Here are the other users of the supplement

Use of PHP strftime ()

String Strftime (String format[,inttimestamp = Time ()])

Returns a string that is formatted with the given timestamp for the specified format. If no timestamp is given, the current local time is used.

Example1:

<?phpecho ' of the year. Strftime ('%W ', Strtotime (' +2 week midnight-0 Second ')). ' Week ';? >

31 Week of the year

Example2:

<?phpecho "Now is the time:". Strftime ('%d.%b%Y%h:%m:%s ', Time ());? >

Now the time is: 20. July 2015 17:01:26

Example3:

<?phpecho ' date is formatted as: '. Strftime ('%d.%b%Y%h:%m:%s ', Strtotime (' 2018-10-15 '));? >

Date is formatted as: 15.October 2018 00:00:00

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.