SoFunction
Updated on 2025-03-10

Detailed explanation of PHP strtotime function

Let’s look at the manual introduction first:

strtotime — parses the date-time description of any English text into a Unix time stamp
Format: int strtotime ( string $time [, int $now ] )
This function expects to accept a string containing the US English date format and attempt to parse it into a Unix timestamp (seconds since January 1 1970 00:00:00 GMT), with a value relative to the time given by the now parameter, and if this parameter is not provided, the system's current time is used.
This function will use the TZ environment variable (if any) to calculate the timestamp. Since PHP 5.1.0, there are easier ways to define the time zone for all date/time functions. This process is described in the date_default_timezone_get() function page.
Note: If the given year is in the format of two digits, its value 0-69 means 2000-2069, and 70-100 means 1970-2000.

parameter
time
The parsed string, formatted according to the syntax of the GNU » Date input format. Before PHP 5.0, milliseconds were not allowed in time. It can be found since PHP 5.0 but will be ignored.
now
The timestamp used to calculate the return value. The default value of this parameter is the current time(), and it can also be set to the timestamp of other times (a function I have always ignored, ashamed)
Return value: If successful, return an interstamp, otherwise return FALSE. Before PHP 5.1.0, this function returns -1 when it fails, and later versions return false.

The first parameter of strtotime can be our common English time format, such as "2008-8-20" or "10 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, which defaults to the current time.
1. Month, Japanese English names and their common abbreviations 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. Detailed description of time parameters and good fortune:
am : the time is before noon
pm : the time is noon or later .
year: one year; for example, "next year" represents next year
month : one month; for example, "last month" month, such as "last month" represents the previous month
fortnight : two weeks; for example, “a fortnight ago” means two weeks ago
week : one week
day: a day
hour: an hour
minute: a minute
min : same as minute Same as minute Same as "minute"
second : a second
sec : same as second Same as second Same as "second"

3. Relevant and order description:
+n/-n: Calculate based on the current time, add or subtract the specified time, for example, "+1 hour" means adding one hour to the current time.
ago :time relative to now; such as “24 hours ago”  Follow the current time, for example, “24 hours ago” means “24 hours ago”
tomorrow : 24 hours later than the current date and time Based on the current time (including date and time) as the standard, the same time tomorrow
yesterday : 24 hours earlier than the current date and time Based on the current time (including date and time), the same time yesterday
today : the current date and time Current time (including date and time)
now : the current date and time Current time (including date and time)
last : modifier meaning "the preceding"; for example, "last tutorial" stands for "the previous one", for example, "last tutorial" stands for "the same time last Tuesday"
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 time, for example, "this 7am" gives the timestamp for 7:00 on the current day, while "this week" gives the timestamp for one week from the current time, and "this week" gives the timestamp for a whole week starting from the current time, that is, the current time (after my test: strtotime('this week')=strtotime('now'));
next : modifier meaning the current time value of the subject plus one; for example, "next hour" refers to the current time plus one hour, which means 3600

// Let's go here first, 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 : 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

There is a function called strtotime in PHP. strtotime implementation function: get the timestamp of a date, or get the timestamp of a certain time. strtotime parses the date-time description of any English text into a Unix timestamp [convert system time to a unix timestamp]

1. Get the Unix timestamp for the specified date

The example of strtotime("2009-1-22") is as follows:
strtotime("2009-1-22")
Result: 1232553600
Description: Return to the 0:00:00 time stamp on January 22, 2009

2. Get the date and time of English text

Examples are as follows:
Easy to compare, use date to convert the time stamp with the specified time stamp into system time
(1) Print the timestamp of tomorrow at this time strtotime("+1 day")
Current time:
date("Y-m-d H:i:s",time())
Results: 2009-01-22 09:40:25
Specified time:
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:
date("Y-m-d H:i:s",time())
Results: 2009-01-22 09:40:25
Specified time:
date("Y-m-d H:i:s",strtotime("-1 day"))
Results: 2009-01-21 09:40:25
(3) Print the timestamp strtotime("+1 week")
Current time:
date("Y-m-d H:i:s",time())
Results: 2009-01-22 09:40:25
Specified time:
date("Y-m-d H:i:s",strtotime("+1 week"))
Results: 2009-01-29 09:40:25
(4) Print the timestamp strtotime("-1 week") from last week
Current time:
date("Y-m-d H:i:s",time())
Results: 2009-01-22 09:40:25
Specified time:
date("Y-m-d H:i:s",strtotime("-1 week"))
Results: 2009-01-15 09:40:25
(5) Print the timestamp of the specified day of the week strtotime("next Thursday")
Current time:
date("Y-m-d H:i:s",time())
Results: 2009-01-22 09:40:25
Specified time:
date("Y-m-d H:i:s",strtotime("next Thursday"))
Results: 2009-01-29 00:00:00
(6) Print the timestamp of the specified day of the week strtotime("last Thursday")
Current time:
date("Y-m-d H:i:s",time())
Results: 2009-01-22 09:40:25
Specified time:
date("Y-m-d H:i:s",strtotime("last Thursday"))
Results: 2009-01-15 00:00:00
As shown in the above example, strtotime can parse the date and time description of any English text into a Unix time stamp. We format the date and time in combination with mktime() or date() to obtain the specified timestamp to achieve the required date and time.
I hope that after the introduction of this article, you can already master the usage of the strtotime function.