Format code supported by the date () function of PHP

Source: Internet
Author: User
Tags echo date
Date--Format a local time/dateDescription

StringDate(String format [, int timestamp])

Returns the integerTimestamp
String generated based on the given format string. If no timestamp is provided, the current local time is used. In other words,Timestamp
Optional. The default value isTime ().

Note:
The valid timestamp typically ranges from 20:45:54 GMT, January 1, December 13, 1901.
By 03:14:07, January 1, January 19, 2038. (This range complies with 32
The minimum and maximum values of signed integers ). In Windows, this range is from January 1, January 1, 1970
May January 19, 2038.

Note:
To convert the time expressed by a string to a timestamp, use
Strtotime (). In addition, some functions in some databases convert their time formats to timestamps (for example
MySQL unix_timestamp function ).

Table 1. The format strings can recognize the followingFormatParameter string

 

FormatCharacter Description Return Value example
A Morning and afternoon values in lower case AMOrPM
A Upper-case morning and afternoon values AMOrPM
B Swatch Internet standard 000To999
D The day of the month, which has two digits leading to zero 01To31
D The day of the week. The text indicates three letters. MonToSun
F The month in the complete text format, such as January or march. JanuaryToDecember
G Hour, 12-hour format, no leading zero 1To12
G Hour, in 24-hour format, no leading zero 0To23
H Hour, 12-hour format, with leading zero 01To12
H Hour, in 24-hour format, with a leading zero 00To23
I Minutes with a leading zero 00To59>
I When it is enabled or not If it is enabled1Otherwise0
J The day of the month, with no leading zero 1To31
L(Lowercase letters of "L) Day of week, complete text format SundayToSaturday
L Is it a leap year? If the leap year is1Otherwise0
M Number indicates the month, with a leading zero 01To12
M The month abbreviated to three characters. JanToDec
N Number indicates the month, with no leading zero 1To12
O Hours different from Greenwich Mean Time For example:+ 0200
R Date in RFC 822 format For example:Thu, 21 Dec 2000 16:01:07 + 0200
S Number of seconds, with a leading zero 00To59>
S English suffix after the number of days per month, 2 Characters St,Nd,RdOrTh. AndJ.
T Number of days in a given month 28To31
T Time zone of the Local Machine For example:EST,MDTIn Windows
For example, "Eastern Standard Time", the Chinese version will display "China Standard Time ").
U Number of seconds since UNIX epoch (January 1 1970 00:00:00 GMT) SeeTime ()
W The day of the week, represented by a number 0(Sunday)6(Saturday)
W The week of the year in ISO-8601 format, starting from Monday each week (New in PHP 4.1.0) For example:42(The first week of the year)
Y The year in which the four digits represent the complete number. For example:1999Or2003
Y A two-digit year For example:99Or03
Z The day of the year. 0To366
Z The number of seconds of the time difference offset. The Time Zone offset to the west of UTC is always negative, and the time zone offset to the east of UTC is always positive. -43200To43200

Unrecognized characters in the format string are displayed as is.ZThe format is in useGmdate ()Always return0.

Example 1.Date () Example

 


<?php
// Prints somethinglike: Wednesday
echo date("l");

 

// Prints something like: wednesday15th of January 2003 05:51:38 AM
Echo date ("l DS of f yh: I: S ");

// Prints: July 1, 2000 is on asaturday
Echo "July 1, 2000 is on a". Date
("L", mktime ));
?>

Escape by adding a backslash before the character in the format string to avoid it being interpreted according to the preceding table. If the character after the backslash is itself a special sequence, escape the backslash.

Example 2:Date () escape characters

 


<?php
// prints somethinglike: Wednesday the 15th
echo date("l \\t\h\ejS");
?>

You can setDate ()And
Mktime ()Used in combination to get future or past dates.


Example 3.
Date () and
Mktime ()Example

 


<?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);
?>

Note:
Because of the timestamp, this method is more reliable than simply adding or subtracting a day or a month's number of seconds on the timestamp.

Some UsageDate ()
Format the date. Note that you need to escape all other characters, because at present, characters with special meanings will produce unwanted results, while other characters in
PHP may be used in future versions. When escaping, use single quotes to avoid conversion of characters similar to \ n into line breaks.

Example 4.
Date () Formatting

 


<?php
// Assuming today is:March 10th, 2001, 5:16:18 pm
$today =date("F j, Y, g:ia");//
March 10, 2001, 5:16pm
$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 is w Day z ');//
05-16-17, 10-03-01,1631 1618 6 Fripm01
$today =date('\i\t \i\s\t\h\e jS \d\a\y.');//
It is the 10thday.
$today=date("D M j G:i:sT Y");//
Sat Mar 1015:16:08 MST 2001
$today =date('H:m:s \m\i\s\ \m\o\n\t\h');//
17:03:17 m ismonth
$today=date("H:i:s");//17:16:17
?>

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.