Date and time in PHP

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

Date

(PHP 4, PHP 5, PHP 7)

date- Formatting a local time/date

Description ¶string Date ( string $format [, int $timestamp ])

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

Tip

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

Tip

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

Note:

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.

Note:

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

format A string of format strings that can recognize the following parameters
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 365
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 >
U Milliseconds (PHP 5.2.2 new Plus). It is important to note that the date () function always returns 000000 because it accepts only the integer parameter, and Datetime::format () supports milliseconds. Example: 654321
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 ().

The date and time in PHP are mainly by the date () function to get and format, although the use of this thing is very high frequency, but sometimes forget or confuse, it is summarized as follows ...

First, year/month/day 1, year

Y (2010): Uppercase y denotes year four digits
Y (10): lowercase y represents two digits of the year

2, Month

F (January): uppercase F denotes full written English for the month (no lowercase f)
M (Jan): Capital M denotes 3 abbreviated characters of the month
M (01-12): lowercase m represents the number of months with leading
N (1-12): lowercase n Indicates the number of months, no leading

3, Japan

J (1-31): Lowercase J Indicates the date of the month, no leading (no uppercase J)
D (01-31): Lowercase d Indicates the date of the month with a leading (no uppercase D)
S (St, ND, RD, TH): uppercase S denotes the suffix of the date, and the date number

4. Example

Y-m-j (2007-02-6)
Y-n-j (07-2-6)
Y-m-j (2007-feb-6)
Y-m-d (2007-02-06)
Y-f-js (2007-february-6th)

Second, time: minute: seconds 1, time

G (1-12): lowercase g denotes 12-hour hours without leading 0
H (01-12): Lowercase h indicates 12-hour hours with leading 0
G (0-23): Uppercase G denotes 24-hour hours without leading 0
H (01-23): Uppercase H denotes 24-hour hours with leading 0

A represents lowercase "am" and "PM" (morning, afternoon)
A denotes uppercase "AM" and "PM"

2, minutes: seconds

By default, the PHP interpretation is displayed as "Greenwich Mean Time", which differs from our local time by 8 hours.

I (1-60): lowercase i denotes minute
S (1-60): lowercase s = seconds

3. Example

G:i:s A (5:56:57 am)
H:i:s A (05:56:57 AM)
G:i:s (14:02:26)

Iii. leap Year, Day of the week, days
    1. Uppercase L Indicates whether this year's leap years, Boolean value, returns 1, otherwise 0
    2. The lowercase w represents the day of the week, and the numeric representation
    3. Uppercase W indicates the number of weeks in a year
    4. Lowercase t indicates how many days are in the current month
    5. Lowercase z indicates that today is the first day of the year
Iv. Other
    1. An uppercase T represents the server's time zone setting, such as a value of UTC
    2. Uppercase I indicates whether the current is daylight saving time, returns 1 for true, otherwise 0
    3. The capital U represents the total number of seconds from January 1, 1970 to the present, which is the UNIX timestamp for the Unix time era.
    4. The lowercase c represents the ISO8601 date, the date format is YYYY-MM-DD, the date and time are separated by the letter T, the time format is HH:MM:SS, and the time zone is expressed as a deviation of Greenwich Mean Time (GMT), such as a value of 2007-02-06t14:24:43+00:00
    5. The lowercase R represents the RFC822 date, if the value is Tue, 2007 14:25:52 +0000
Top
0

Date and time in PHP

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.