Home >Backend Development >PHP Problem >How to get the timestamp of previous days in php

How to get the timestamp of previous days in php

青灯夜游
青灯夜游Original
2022-01-10 18:35:503095browse

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.

How to get the timestamp of previous days in php

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(&#39;content-type:text/html;charset=utf-8&#39;);
echo "今天的时间戳:".time(); 
echo "<br>前两天的时间戳:".strtotime("-2 day"); //前一天 
echo "<br>前一天的时间戳:".strtotime("-1 day"); //前一天 
echo "<br>后一天的时间戳:".strtotime("+1 day"); //后一天 
?>

Output:

How to get the timestamp of previous days in php

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(&#39;content-type:text/html;charset=utf-8&#39;);
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")); //后一天 
?>

How to get the timestamp of previous days in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn