How to Get Yesterday Date using PHP?
Working with dates is one of the most common tasks in PHP. Whether you are building a reporting system, scheduling events, or tracking user activity, you may need to find the previous date. PHP makes this easy with its built-in date() and strtotime() functions.
In this tutorial, we will look at two simple ways to get yesterday’s date using PHP:
- From the current date
- From a given date
Both approaches are quick and beginner-friendly.
Get Yesterday’s Date From the Current Date
The simplest way to find yesterday is to subtract one day from the current date using strtotime('-1 day').
<?php
$new_date = date('Y-m-d', strtotime('-1 day'));
echo $new_date;
?>
Output
2021-11-27
Here:
strtotime('-1 day')tells PHP to calculate the date one day earlier.date('Y-m-d', …)formats the output in YYYY-MM-DD, which is ideal for databases and logs.
Get Yesterday’s Date Using the Keyword “yesterday”
PHP also understands human-readable keywords like yesterday, tomorrow, and next month. This makes code cleaner and more readable.
<?php
$new_date = date('Y-m-d', strtotime('yesterday'));
echo $new_date;
?>
Output
2021-11-27
Both examples return the same result. Use whichever style you prefer.
Final Thoughts
PHP provides flexible date handling, and calculating yesterday’s date is very straightforward. Using date() with strtotime() lets you retrieve dates without complex math or external libraries.