How to get date several days ago with PHP

PHPz
Release: 2023-04-25 18:41:54
Original
1703 people have browsed it

PHP is a widely used server-side scripting language used for web development and other applications. In web development, we often need to get the date and time to display information on the website. In some cases we need to get a date that is a few days ago for some reasons. So, how do we get the date a few days ago using PHP?

PHP provides a series of built-in functions to handle dates and times. Among them, the most commonly used functions are date() and strtotime(). We can use these two functions to get the date a few days ago.

Now, let’s explain in detail how to use these two functions to get the date a few days ago.

Use the date() function to get the date a few days ago

The date() function can format the timestamp into a specified date and time string. We can use the date() function to get the date a few days ago.

Here is a sample code that gets yesterday’s date:

$date = date('Y-m-d', strtotime('-1 day'));
echo $date;
Copy after login

In the above code, we used the strtotime() function to get yesterday’s timestamp , and then use the date() function to format the timestamp into the specified date string. -1 day means subtracting one day, so this code will output yesterday’s date string in the format YYYY-MM-DD.

We can modify -1 day in the code to get the date any few days ago. For example, if we want to get the date 7 days ago, we only need to change -1 day in the code to -7 days, the code is as follows:

$date = date('Y-m-d', strtotime('-7 days'));
echo $date;
Copy after login

This The code will output the date string 7 days ago in the format YYYY-MM-DD.

Use the strtotime() function to get the date a few days ago

strtotime()The function can convert any human-readable date-time string into the corresponding timestamp . We can use the strtotime() function to get the date a few days ago.

Here is a sample code that gets the date 5 days ago:

$date = date('Y-m-d', strtotime('-5 days'));
echo $date;
Copy after login

In the above code, we used the strtotime() function to get 5 days The previous timestamp, and then use the date() function to format the timestamp into the specified date string. -5 days means minus five days, so this code will output the date string 5 days ago in the format YYYY-MM-DD.

Like the previous example, we can change the date value in the code to get the date any number of days ago.

Summary

By using PHP’s built-in functions date() and strtotime(), we can easily get the date any few days ago . We can change the date value in the code as needed.

In actual development, we often need to use date and time operations, so it is very important to understand how to use PHP functions to operate date and time. Whether you are a beginner or an experienced developer, these functions are very useful.

The above is the detailed content of How to get date several days ago with PHP. For more information, please follow other related articles on the PHP Chinese website!

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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!