Home  >  Article  >  Backend Development  >  How to get the date of previous days in php

How to get the date of previous days in php

青灯夜游
青灯夜游Original
2021-03-25 18:50:345105browse

In PHP, you can use the date() and strtotime() functions to get the dates of the previous few days. The syntax format is "date("Y-m-d", strtotime("-x day"))"; parameter x Used to specify the number of days. When the value is 1, the date of the previous 1 day is obtained. When the value is 2, the date of the previous 2 days is obtained.

How to get the date of previous days in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php gets the date of the previous few days

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);
echo "前两天的日期:".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")); //后一天 
?>

Output:

前两天的日期:2021-03-23
前一天的日期:2021-03-24
后一天的日期:2021-03-26

Related function introduction:

PHP date() function can put the timestamp Format the date and time to be more readable.

The strtotime() function parses any English text date or time description into a Unix timestamp (number of seconds since January 1 1970 00:00:00 GMT).

Syntax

strtotime(time,now);
Parameters Description
time Required. Specifies a date/time string.
now Optional. Specifies the timestamp used to calculate the return value. If this parameter is omitted, the current time is used.

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to get the date 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