PHP strtotime function

  • 2020-03-31 20:04:37
  • OfStack

Read the manual first:

Strtotime - parses any date-time description of English text into a Unix timestamp
Format: int strtotime (string $time [, int $now])
This function expects to take a string containing an American English date format and attempt to parse it into a Unix timestamp (seconds from January 1 1970 00:00:00 GMT), with a value relative to the time given by the now parameter, or the current system time if not provided.
This function USES the TZ environment variable, if any, to calculate the timestamp. Since PHP 5.1.0 there has been an easier way to define time zones for all date/time functions. This procedure is described in the date_default_timezone_get() function page.
Note: if the given year is in a two-digit format, its value 0-69 means 2000-2069, and 70-100 means 1970-2000.

parameter
time
Parsed strings, format according to GNU » Syntax for date input format. Before PHP 5.0, the number of milliseconds in time was not allowed. Since PHP 5.0, it is, but is ignored.
now
To calculate the timestamp for the return value. The default value of this parameter is the current time(), which can also be set to timestamps for other times (shame on me for ignoring this feature).
Return value: returns an interval stamp on success or FALSE otherwise. Before PHP 5.1.0, this function returned -1 on failure and false in later versions.

The first parameter of strtotime can be our common English time format, such as "2008-8-20" or "10 September 2000". It can also be a time description based on the parameter now, such as "+1 day", etc.


The following is a list of available parameters for the latter, where "current time" refers to the value of strtotime's second parameter now, which defaults to the current time
1. List of English names of month and day and their commonly used abbreviations:
January, February, march, April, may, June, out, August, September, sept, October, November, another awarding,
Sunday, Monday, Tuesday, Wednesday, tues, wednes, Thursday, thur, thurs, Friday, Saturday

2. Time parameters and detailed description:
Am: the time is before noon
PM: the time is noon or later afternoon
Year: one year; For example, "next year" means next year
The month: one month; For example, "last month" means last month
Fortnight: two weekes; For example, "a fortnight line two weeks", such as "a fortnight line" represent two weeks ago
Week: one week
Day: a day day
-leonard: it's an hour
-penny: minute, a minute
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
-penny: a second
Secant: same as second

3. Description of correlation and sequence:
+n/-n: take the current time and add or subtract the specified time. For example, "+1 hour" means the current time plus one hour
Ago :time relative to now; Such as "24 hours ago" is calculated in terms of the current time. For example, "24 hours ago" stands for "24 hours ago".
Tomorrow: 24 hours later than the current date and time
Yesterday: 24 hours earlier than the current date and time
Today: the current date and time
Now: the current date and time
The last: modifier fancy "the preceding"; For example, "last Tuesday" stands for "last Tuesday", for example, "last Tuesday at the same time"
This: the given time during the current day or the next occurrence of the given time; For example, "this 7am" gives the timestamp for 07:00 on the current day, while "this week" gives the timestamp for one week from the current day or the timestamp for the next period. For example, "this 7am" gives the timestamp for 7:00 on the current day, "This week" gives a timestamp of the entire week from the current time, which is the current time(in my test: strtotime('this week')=strtotime('now'));
Next: modifier meaning the current time value of the subject plus one; For example, "next hour" means the current time plus the specified time. For example, "next hour" means the current time plus one hour, plus 3,600

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

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: alk-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
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, COBOL Zone 9
NZT: New Zealand Time
NZST: New Zealand Standard Time
NZDT: New Zealand Daylight Time
Idle: International Date Line East

There's a function in PHP called strtotime. Strtotime implementation: get a timestamp for a date, or get a timestamp for a time. Strtotime parses the date time description of any English text into a Unix timestamp [converts system time to a Unix timestamp]

One, gets 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: returns the time stamp of January 22, 2009, 0:0 minutes, 0 seconds

Second, get the English text date time

Here's an example:
For comparison purposes, use date to convert when timestamp to system time with the specified timestamp
(1) print the time stamp of this time tomorrow strtotime("+1 day")
Current time:
1. Echo date (" Y -m - d H: I: s ", time ())
Results: 2009-01-22 09:40:25
Designated time:
1. Echo date (" Y -m - d H: I: s ", strtotime (" + 1 day "))
Results: 2009-01-23 09:40:25
(2) print the time stamp strtotime("-1 day")
Current time:
1. Echo date (" Y -m - d H: I: s ", time ())
Results: 2009-01-22 09:40:25
Designated time:
1. Echo date (" Y -m - d H: I: s ", strtotime (" 1 day "))
Results: 2009-01-21 09:40:25
(3) print the time stamp of next week at this time strtotime("+1 week")
Current time:
1. Echo date (" Y -m - d H: I: s ", time ())
Results: 2009-01-22 09:40:25
Designated time:
1. Echo date (" Y -m - d H: I: s ", strtotime (" + 1 week "))
Results: 2009-01-29 09:40:25
(4) print the time stamp of last week at this time strtotime("-1 week")
Current time:
1. Echo date (" Y -m - d H: I: s ", time ())
Results: 2009-01-22 09:40:25
Designated time:
1. Echo date (" Y -m - d H: I: s ", strtotime (" 1 week "))
Results: 2009-01-15 09:40:25
(5) print strtotime("next Thursday")
Current time:
1. Echo date (" Y -m - d H: I: s ", time ())
Results: 2009-01-22 09:40:25
Designated time:
1. Echo date (" Y -m - d H: I: s ", strtotime (" next Thursday "))
Results: 2009-01-29 00:00:00
(6) print strtotime("last Thursday")
Current time:
1. Echo date (" Y -m - d H: I: s ", time ())
Results: 2009-01-22 09:40:25
Designated 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 parse the date and time description of any English text into a Unix timestamp, and we can combine mktime() or date() to format the date time to get the specified timestamp and realize the required date and time.
Hopefully, by the end of this article, you've got a handle on strtotime.

Related articles: