PHP Dates and Times
Printing a Date or Time in a Specified Format
Problem
You need to print out a date or time formatted in a particular way.Solution
Example 3-13. Using date() and DateTime::format( )print date('d/M/Y') . "\n";
$when = new DateTime();
print $when->format('d/M/Y');
Example 3-13 prints something like:
06/Feb/2013
06/Feb/2013
Discussion
Both date() and DateTime::format() use the same code internally for generating formatted time and date strings. They are flexible functions that can produce a formatted time string with a variety of components. The format characters for these functions are listed in TableTable date() format characters
Type Character Description Range or examples
Hour H Hour, numeric, 24-hour clock, leading zero 00–23
Hour h Hour, numeric, 12-hour clock, leading zero 01–12
Hour G Hour, numeric, 24-hour clock 0–23
Hour g Hour, numeric, 12-hour clock 1–12
Hour A Ante/Post Meridiem designation AM, PM
Hour a Ante/Post Meridiem designation am, pm
Minute i Minute, numeric 00–59
Second s Second, numeric 00–59
Second u Microseconds, string 000000–999999
Day d Day of the month, numeric, leading zero 01–31
Day j Day of the month, numeric 1–31
Day z Day of the year, numeric 0–365
Day N Day of the week, numeric (Monday is 1) 1–7
________________________________________________________________________________
Type Character Description Range or examples
Day w Day of the week, numeric (Sunday is 0) 0–6
Day S Englishordinalsuffixfordayofthemonth,textual “st,” “th,” “nd,” “rd”
Week D Abbreviated weekday name Mon,Tue,Wed,Thu,Fri,Sat,Sun
Week l Full weekday name Monday, Tuesday, Wednesday Thursday, Friday,Saturday, Sunday
Week W ISO8601:1988weeknumberintheyear,numeric,
week 1 is the first week that has at least 4
days in the current year, Monday is the first
day of the week 1–53
days in the current year, Monday is the first
day of the week 1–53
Month F Full month name January–December
Month M Abbreviated month name Jan–Dec
Month m Month, numeric, leading zero 01–12
Month n Month, numeric 1–12
Month t Month length in days, numeric 28, 29, 30, 31
Year Y Year, numeric, including century e.g., 2016
Year y Year without century, numeric e.g., 16
Year o ISO 8601 year with century; numeric; the
four-digityearcorrespondingtotheISOweek
number;sameas Y exceptiftheISOweek
numberbelongs to the previous or next year,
that year is used instead e.g. 2016
four-digityearcorrespondingtotheISOweek
number;sameas Y exceptiftheISOweek
numberbelongs to the previous or next year,
that year is used instead e.g. 2016
Year L Leap year flag (yes is 1) 0, 1
Time zone O Hour offset from GMT, ±HHMM (e.g.,
−0400,+0230) −1200–+1200
−0400,+0230) −1200–+1200
Time zone P Like O, but with a colon −12:00 –+12:00
Time zone Z SecondsoffsetfromGMT;westofGMTis
negative,east of GMT is positive -43200–50400
negative,east of GMT is positive -43200–50400
Time zone e Time zone identifier e.g., America/New_York
Time zone T Time zone abbreviation e.g., EDT
Time zone I Daylight saving time flag (yes is 1) 0, 1
Compound c ISO 8601–formatted date and time e.g., 2012-09-06T15:29:34+0000
Compound r RFC 2822–formatted date e.g., Thu, 22 Aug 2002 16:01:07
+0200
Other U Seconds since the Unix epoch 0−2147483647
Other B Swatch Internet time 000–999
_________________________________________________________________________________
Format characters such as F, M, or D, which generate words, not numbers, produce output in English.
Format characters such as F, M, or D, which generate words, not numbers, produce output in English.
There are also some handy constants for common date formats that represent the format string to be passed to date() or DateTime::format(). These constants are listed in Table.
Table Constants for use with date()
No comments:
Post a Comment