Home >Backend Development >PHP Problem >How to get the timestamp of previous days in php
In php, you can use the strtotime() function to get the timestamps of the previous few days. This function can add and subtract dates, calculate the intervals between some dates and times, and convert the interval dates in UNIX The format of the timestamp is returned; the syntax is "strtotime("-x day")", and the parameter x is used to specify the number of days in the interval.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In PHP, you can use the strtotime() function to get timestamps from previous days.
Sometimes we need to add or subtract a certain time interval from a date. You can use strtotime() to calculate some date time intervals and return the interval dates in UNIX timestamp format.
strtotime() function has two uses: one is to parse the date and time in the form of a string and described in English text into a UNIX timestamp, and the other is to calculate some date and time interval.
Example:
<?php header('content-type:text/html;charset=utf-8'); echo "今天的时间戳:".time(); echo "<br>前两天的时间戳:".strtotime("-2 day"); //前一天 echo "<br>前一天的时间戳:".strtotime("-1 day"); //前一天 echo "<br>后一天的时间戳:".strtotime("+1 day"); //后一天 ?>
Output:
Is it difficult to compare? We use the date() function to format it Convert it to year, month and day format, let’s take a closer look:
<?php header('content-type:text/html;charset=utf-8'); echo "今天的日期:".date("Y-m-d", time()); echo "<br>前两天的日期:".date("Y-m-d", strtotime("-2 day")); //前一天 echo "<br>前一天的日期:".date("Y-m-d", strtotime("-1 day")); //前一天 echo "<br>后一天的日期:".date("Y-m-d", strtotime("+1 day")); //后一天 ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to get the timestamp of previous days in php. For more information, please follow other related articles on the PHP Chinese website!