In PHP the strtotime function is explained in detail

  • 2020-05-10 17:48:53
  • OfStack

In PHP there is a function called strtotime. strtotime implements: get a timestamp for a date, or get a timestamp for a time. strtotime parses the date and time description of any English text into an Unix timestamp [converts system time to an unix timestamp]

1, get the unix timestamp for the specified date

Examples of strtotime("2009-1-22") are as follows:
1.echo strtotime("2009-1-22")
Results: 1232553600
Note: returns a timestamp of 0:0:0:0:00pm, January 22, 2009

2. Get the English text date and time

Here's an example:
For comparison purposes, use date to convert the time stamp from the specified time stamp to the system time
(1) print time stamp strtotime("+1 day") at this time tomorrow
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") of yesterday at this time.
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 strtotime("+1 week") for the next week at this time
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 strtotime("-1 week") of the previous week at this time.
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 a timestamp strtotime("next Thursday") specifying the date of the following 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("next Thursday"))
Results: 2009-01-29 00:00:00
(6) print the timestamp strtotime("last Thursday") for the specified date of last 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("last Thursday"))
Results: 2009-01-15 00:00:00
As can be seen from the above example, strtotime can parse the date and time description of any English text into Unix timestamp, and we can get the specified timestamp in combination with mktime() or date() to format the date and time, so as to realize the required date and time.
Hopefully, after the introduction of this article, you have mastered the usage of strtotime function.

Related articles: