PHP strtotime function Detailed _php tips

Source: Internet
Author: User
Tags current time echo date modifier
First look at the manual introduction:

strtotime-resolves a date-time description of any English text to a Unix timestamp
Format: int strtotime (string $time [, int $now])
This function expects to accept a string containing the U.S. English date format and tries to resolve it to a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT), with a value relative to the time given by the now parameter, or the current time of the system if this parameter is not supplied.
This function uses the TZ environment variable (if any) to compute the timestamp. There are easier ways to define a time zone for all date/time functions from PHP 5.1.0. This procedure is described in the Date_default_timezone_get () function page.
Note: If the given year is a two-digit format, its value of 0-69 indicates that 2000-2069,70-100 represents 1970-2000.

Parameters
Time
The parsed string that formats the syntax according to the gnu» date input format. Before PHP 5.0, the time is not allowed to have milliseconds, since PHP 5.0 can be, but will be ignored.
Now
The timestamp used to calculate the return value. The default value for this parameter is the current time (), or it can be set to a time stamp of another time (I have been ignoring a feature ah, ashamed)
Return value: Success returns the inter-stamp, otherwise returns FALSE. This function returns 1 on failure before PHP 5.1.0, and the later version returns false.

Strtotime's first parameter can be our common English time format, such as "2008-8-20" or "Ten September 2000" and so on. It can also be a time description based on the parameter now, such as "+1 Day", and so on.


The following is a list of available parameters in the latter way, where "current time" refers to the value of the second parameter now Strtotime, which defaults to the current time
1. Month, day English name and its common abbreviation list:
January,february,march,april,may,june,july,august,september,sept,october,november,december,
Sunday,monday,tuesday,tues,wednesday,wednes,thursday,thur,thurs,friday,saturday

2. Time parameter Cheung Fine Description:
Am:the time is before noon a.m.
Pm:the is noon or later PM
Year:one year; For example, "next year", such as "next year" represents
Month:one month; For example, the "last month" month, for example, "month" on behalf of
Fortnight:two weeks; For example, "a fortnight ago" two weeks, for example, "a fortnight ago" represents two weeks ago
Week:one Week Week
Day:a Day
Hour:an Hour Hours
Minute:a minute minutes
Min:same as minute with "minute"
second:a second seconds
Sec:same as second with "second"

3. Relevant and Sequential description:
+n/-n: In the current time, add a minus the specified time, such as "+1 hour" refers to the current time plus an hour
Ago:time relative to now; such as "hours ago" in the current time forward, such as "hours Ago" represents "24 hours before"
Tomorrow:24 hours later than the current date and time is the standard for the present time (including the dates and times).
Yesterday:24 hours earlier than the current date and time is the standard for the present time (including dates and times) yesterday
Today:the Current date and time (including date and time)
Now:the Current date and time (including date and time)
Last:modifier meaning "the preceding"; For example, "Last Tuesday" represents "previous", such as "last Tuesday" represents "the same time in Tuesday"
This:the given time during the "current" or "next" occurrence of the given time; For example, ' This 7am ' gives the ' timestamp for ' 07:00 on the ' this ', while ' this week ' gives ' for one we The time stamp for a specified time or a period of time on the current day of the EK from, for example, "This 7am" gives the 7:00 timestamp of the day, and "This week" gives a whole week's time stamp starting at the present time. That is the current time (after I test: strtotime (' this Week ') =strtotime (' Now '));
Next:modifier meaning the current time value of the subject plus one; For example, "next hour" the current time plus the specified time, such as "next hour" means the current time plus an hour, plus 3600

First here, there's no time to translate
First:ordinal modifier, esp. for months; For example, "may I" (actually, it's just the same as next)
Third:see The There is no "second" for ordinality, since that would conflict with the second time value)
Fourth:see
Fifth:see
Sixth:see
Seventh:see
Eighth:see
Ninth:see
Tenth:see
Eleventh:see
Twelfth:see

4. Time Zone Description:
Gmt:greenwich Mean Time
Ut:coordinated Universal Time
Utc:same as UT
Wet:western European Time
Bst:british Summer Time
Wat:west Africa Time
At:azores time
Ast:atlantic Standard Time
Adt:atlantic Daylight Time
Est:eastern Standard Time
Edt:eastern Daylight Time
Cst:central Standard Time
Cdt:central Daylight Time
Mst:mountain Standard Time
Mdt:mountain Daylight Time
Pst:pacific Standard Time
Pdt:pacific Daylight Time
Yst:yukon Standard Time
Ydt:yukon Daylight Time
Hst:hawaii Standard Time
Hdt:hawaii Daylight Time
Cat:central Alaska Time
Akst:alaska Standard Time
Akdt:alaska Daylight Time
Ahst:alaska-hawaii Standard Time
Nt:nome time
Idlw:international Date Line West
Cet:central European Time
Met:middle European Time
Mewt:middle European Winter Time
Mest:middle European Summer Time
Mesz:middle European Summer Time
Swt:swedish Winter Time
Sst:swedish Summer Time
Fwt:french Winter Time
Fst:french Summer Time
Eet:eastern Europe Time, USSR Zone 1
Bt:baghdad time, USSR Zone 2
ZP4:USSR Zone 3
ZP5:USSR Zone 4
ZP6:USSR Zone 5
Wast:west Australian Standard Time
Wadt:west Australian Daylight Time
Cct:china Coast time, USSR Zone 7
Jst:japan Standard time, USSR Zone 8
East:eastern Australian Standard Time
Eadt:eastern Australian Daylight Time
Gst:guam Standard Time, USSR Zone 9
Nzt:new Zealand Time
Nzst:new Zealand Standard Time
Nzdt:new Zealand Daylight Time
Idle:international Date Line East

In PHP, there is a function called Strtotime. Strtotime implementation: Gets the timestamp of a date, or gets a timestamp of a time. strtotime resolves a date-time description of any English text to a UNIX timestamp [converts system time to UNIX timestamp]

first, get the UNIX timestamp for the specified date

The Strtotime ("2009-1-22") example is as follows:
1.echo strtotime ("2009-1-22")
Results: 1232553600
Note: Return January 22, 2009 0:0 0 seconds time stamp

two, get English text date time

Examples are as follows:
Easy to compare, using date to convert the timestamp to the system time with the specified timestamp
(1) Print tomorrow at this time the timestamp strtotime ("+1 Day")
Current time:
1.echo date ("Y-m-d h:i:s", Time ())
Results: 2009-01-22 09:40:25
Specified time:
1.echo date ("Y-m-d h:i:s", Strtotime ("+1 Day"))
Results: 2009-01-23 09:40:25
(2) Print the timestamp strtotime ("-1 day") at this time yesterday
Current time:
1.echo date ("Y-m-d h:i:s", Time ())
Results: 2009-01-22 09:40:25
Specified time:
1.echo date ("Y-m-d h:i:s", Strtotime ("-1 day"))
Results: 2009-01-21 09:40:25
(3) Print the timestamp strtotime ("+1 Week") at this time next week
Current time:
1.echo date ("Y-m-d h:i:s", Time ())
Results: 2009-01-22 09:40:25
Specified time:
1.echo date ("Y-m-d h:i:s", Strtotime ("+1 Week"))
Results: 2009-01-29 09:40:25
(4) Print the timestamp strtotime ("-1 week") at this time last week
Current time:
1.echo date ("Y-m-d h:i:s", Time ())
Results: 2009-01-22 09:40:25
Specified time:
1.echo date ("Y-m-d h:i:s", Strtotime ("-1 Week"))
Results: 2009-01-15 09:40:25
(5) Print a timestamp strtotime ("next Thursday") that specifies the next day of the week
Current time:
1.echo date ("Y-m-d h:i:s", Time ())
Results: 2009-01-22 09:40:25
Specified time:
1.echo date ("Y-m-d h:i:s", Strtotime ("next Thursday")
Results: 2009-01-29 00:00:00
(6) Print a timestamp strtotime ("last Thursday") that specifies the previous week
Current time:
1.echo date ("Y-m-d h:i:s", Time ())
Results: 2009-01-22 09:40:25
Specified time:
1.echo date ("Y-m-d h:i:s", Strtotime ("last Thursday")
Results: 2009-01-15 00:00:00
The above example shows that Strtotime can resolve the date-time description of any English text to a Unix timestamp, and we combine the mktime () or date () format date time to get the specified timestamp to achieve the required DateTime.
Hopefully, after the introduction of this article, you have mastered the Strtotime function usage.

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.