PHP date Format a local time/date

Source: Internet
Author: User
Tags iso 8601 iso 8601 format local time

PHP date format a local time date

(PHP 4, PHP 5)

date- Formatting a local time/date

Descriptionstring date ( string $format [, int $timestamp ])

Returns a string that is the result of the timestamp of an integer in a given format string. If no timestamp is given, the local current time is used. In other words,timestamp is optional and the default value is Time ().


Since PHP 5.1.1 There are several useful constants that can be used as standard date/time formats to specify the format parameter.


The timestamp at the time $_SERVER[‘REQUEST_TIME‘] the request was initiated has been saved from PHP 5.1.


A valid timestamp typically ranges from 20:45:54 GMT December 13, 1901 to January 19, 2038 03:14:07. (This range conforms to the minimum and maximum values of a 32-bit signed integer). However, prior to PHP 5.1, this range was limited to some systems (such as Windows) from January 1, 1970 to January 19, 2038.


To convert the time that a string is expressed to a timestamp, you should use strtotime (). In addition some databases have some functions to convert their time format into timestamps (such as the»unix_timestamp function of MySQL).

The format string can recognize the following format parameter Strings
format character description return value Example
Day --- ---
D The day ordinal of a month with a leading zero 2-digit number to
D Day of the week, text representation, 3 letters Mon to Sun
J The day ordinal of the month without leading zeros 1 to
l(lowercase letter of "L") Day of the week, full text format Sunday to Saturday
N ISO-8601 format number represents the day of the week (PHP 5.1.0 new Plus) 1(for Monday) to 7(= Sunday)
S English suffix, 2 characters, after the number of days per month St,nd,Rd or th. Can be used with J .
W Day of the week, numbers indicate 0(for Sunday) to 6(= Saturday)
Z The day ordinal of the year 0 to 366
Week --- ---
W Week of the ISO-8601 format year, starting from Monday (PHP 4.1.0 New) For example: (42nd week of the year)
Month --- ---
F month, full text format, such as January or March January to December
M A number represents the month, with a leading zero to
M Three letter abbreviation for month Jan to Dec
N The number represents the month, without leading zeros 1 to
T The number of days that a given month should be from
Years --- ---
L Whether it is a leap year If the leap year is 1, otherwise 0
O ISO-8601 format year number. This is the same value as Y , except if the day of the week (W) of the ISO belongs to the previous year or the next year. (PHP 5.1.0 new addition) Examples: 1999 or 2003
Y 4-digit year in full representation Example:1999 or 2003
Y Year 2-digit representation For example :
Time --- ---
A Lowercase morning and afternoon values am or pm
A Uppercase morning and afternoon values AM or PM
B Swatch Internet Standard Time to 999
G Hours, 12-hour format, no leading zeros 1 to
G Hours, 24-hour format, no leading zeros 0 to
H Hours, 12-hour format, with leading zeros to
H Hours, 24-hour format, with leading zeros From xx to
I Number of minutes with leading zeros From xx to >
S Number of seconds with leading zeros From xx to >
Time --- ---
E Time zone ID (PHP 5.1.0 new addition) For example:UTC,GMT,atlantic/azores
I Whether it is daylight saving time If daylight saving time is 1, otherwise 0
O Hours of difference from GMT Example:+0200
P Difference from GMT (GMT), between hours and minutes colon separated (PHP 5.1.3 New) Example:+02:00
T The time zone in which this machine resides For example:EST,MDT(the "translator note" is in full text format under Windows, such as "Eastern Standard Time", Chinese version will show "China normal times").
Z The number of seconds of the slack offset. The time zone offset in the west of UTC is always negative, and the time zone offset to the east of UTC is always positive. -43200 to 43200
Full Date/Time --- ---
C Date in ISO 8601 format (PHP 5 new) 2004-02-12t15:19:21+00:00
R Date in RFC 822 format For example:Thu, Dec 16:01:07 +0200
U The number of seconds since the Unix era (January 1 1970 00:00:00 GMT) started See Time ()

Characters that are not recognized in the format string are displayed as-is. The Z format always returns 0when using gmdate ().

Example #1 date () example

< ? PHP
//  Sets the default time zone to use. From  PHP 5.1  available
Date_default_timezone_set (' UTC ');

//  output is similar to the following: Monday
Echo date ("L");

//  output similar to: MONDAY&NBSP;15TH&NBSP;OF&NBSP;AUGUST&NBSP;2005&NBSP;03:12:46&NBSP;PM
Echo date (' l  ds of f y h:i:s a ');

//  output: July 1, 2000 is on a saturday
echo  "July 1,  2000 is on a  " . date (" L ",  mktime (0, 0, 0, 7, 1,  2000));

/*  Use constants in format parameters  */
//  output similar to: MON,&NBSP;15&NBSP;AUG&NBSP;2005&NBSP;15:12:46&NBSP;UTC
Echo date (date_rfc822); The

//  output is similar to the following: 2000-07-01t00:00:00+00:00
Echo date (Date_atom, mktime (0, 0, 0,  7, 1, 2000));

Escaping a character in a format string preceded by a backslash prevents it from being interpreted as described in the preceding table. If the character after the backslash is a special sequence, it also escapes the backslash.

Example #2 the literal character in date ()

// prints something like: Wednesday the 15th
echo date("l \the jS");

You can use date () and mktime () together to get a date in the future or past.

Example #3 date () and mktime () examples

< Span class= "Phpcode" > <?php
$tomorrow   = mktime (0, 0, 0, date ("M")    , date ("D") +1, date ("Y"));
$lastmonth  = mktime (0, 0, 0, date ("M") -1, date ("D"),    date (" Y "));
$nextyear   = mktime (0, 0, 0, date ("M"),    date ("D"),    date ("Y") +1);


Due to daylight saving time, this method is more reliable than simply adding a day or one-month-long seconds to the timestamp.

Some examples of using date () to format dates. Note that all other characters are escaped, because characters with special meanings now produce unwanted results, and the remaining characters may be used in future versions of PHP. When escaping, note that single quotation marks are used to avoid similar characters becoming line breaks.

Example #4 date () format example

Assume today is: March 10th, 2001, 5:16:18 PM
$today = Date ("F J, Y, g:i a"); March, 2001, 5:16 PM
$today = Date ("M.d.y"); 03.10.01
$today = Date ("J, N, Y"); 10, 3, 2001
$today = Date ("Ymd"); 20010310
$today = Date (' H-i-s, J-m-y, it's W day Z '); 05-16-17, 10-03-01, 1631 1618 6 Fripm01
$today = Date (' I was he jS day. '); It's the 10th day.
$today = Date ("D M J g:i:s T Y"); Sat Mar 15:16:08 MST 2001
$today = Date (' h:m:s m is Mo H '); 17:03:17 M is month
$today = Date ("H:i:s"); 17:16:17

To format dates for other languages, you should use the setlocale () and strftime () functions instead of date ().

See Getlastmod (),gmdate (),mktime (),strftime (), and Time ().

PHP date Format a local time/date

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: 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.