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

How to get the date of previous days in php

青灯夜游
Release: 2023-03-09 06:26:01
Original
5191 people have browsed it

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")); //后一天 
?>
Copy after login

Output:

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

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);
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template