Parse php timestamp and date conversion

  • 2020-06-07 04:04:47
  • OfStack

You probably already know about the PHP timestamp, so how do we use it to get specific dates? Today we are going to show you how PHP timestamp gets the current period.

Implementation: Get a timestamp for a date, or an PHP timestamp for a time.

strtotime can parse the date-time description of any English text into an Unix timestamp, and we combine mktime() or date() formatting date time to get the specified timestamp to achieve the required date time.

strtotime parses the date-time description of any English text as an Unix timestamp [converts system time to unix timestamp]

1, get the unix timestamp strtotime(" 2009-1-22 ") for the specified date as follows:
Results: 1232553600
Description: Return the 0:0:0 second timestamp on January 22, 2009

2. Get English text Date and time example as follows:
For comparison purposes, date is used to convert when timestamp to the specified timestamp into system time

(1) Print the timestamp strtotime(" +1 day ") at this time tomorrow
echo date(" Y-ES37en-ES38en H:i:s ",time()
Time: echo date(" ES46en-ES47en-ES48en H: s ",strtotime(" +1 day ")) result: 2009-01-23 09:40:25

(2) Print the PHP timestamp strtotime(" -1 day ") at this time yesterday.
echo date(" ES62en-ES63en-ES64en i:s ",time()) results: 2009-01-22 09:40:25
Time: echo date(" ES72en-ES73en-ES74en :i:s ",strtotime(" -1 day ")) Result: 2009-01-21 09:40:25

(3) Print the timestamp strtotime(" +1 week ") at this time next week
Results: 2009-01-22 09:40:25
Time: echo date(" Y-ES98en-d H:i:s ",strtotime(" +1 week ")) Result: 2009-01-29 09:40:25

(4) Print the timestamp strtotime(" -1 week ") at this time last week
echo date(" ES112en-ES113en-ES115en :i:s ",time()) results: 2009-01-22 09:40:25
Time: echo date(" ES122en-ES123en-ES124en ",strtotime(" -1 week ")) result: 2009-01-15 09:40:25

(5) Print PHP timestamp strtotime(" next Thursday ") specifying the day of next week
echo date(" ES139en-ES140en-ES141en ",time()) results: 2009-01-22 09:40:25
Time: echo date(" ES149en-ES150en-ES151en H: s ",strtotime(" next Thursday ")) result: 2009-01-29 00:00:00

(6) Print a timestamp strtotime(" last Thursday ") specifying the day of last week
echo date(" ES166en-ES167en-ES168en ",time()) results: 2009-01-22 09:40:25
Time: echo date(" ES176en-ES177en-ES178en H: s ",strtotime(" last Thursday ")) result: 2009-01-15 00:00:00

As the above example shows, strtotime can parse the date-time description of any English text into an Unix timestamp. We combine mktime() or date() formatting date time to get the specified PHP timestamp to achieve the required date time.


Related articles: