how to get date of yesterday using php?

how to get date of yesterday using php?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Getting Yesterday's Date in PHP: A Practical Guide

When you need to manipulate dates in PHP, the core mechanism is straightforward, but handling edge cases like month rollovers or leap years requires a bit more nuance than simply subtracting one day from a string representation. If your goal is to retrieve yesterday's date formatted as d.m.Y (for example, 15.04.2013), you should avoid performing arithmetic directly on the formatted string. String manipulation for dates is brittle and prone to errors when crossing month boundaries or handling varying day counts in February.

The robust approach relies on PHP's built-in DateTime class or the legacy strtotime function combined with the date() function. These tools handle the complexity of the Gregorian calendar internally, ensuring that if today is January 1st, yesterday correctly calculates to December 31st of the previous year without you needing to write conditional logic for month changes.

The Modern Approach Using DateTime

In modern PHP development (version 5.5 and above), the DateTime class is the preferred method because it offers type safety and a clean API. You can create an instance representing today, subtract one day using the modify() method, and then format the result.

Here is how to implement this for your specific format:

$today = new DateTime();
$yesterday = clone $today; // Create a copy to avoid modifying the original 'today' object

$yesterday->modify('-1 day');

$formattedYesterday = $yesterday->format('d.m.Y');

echo $formattedYesterday; // Outputs: e.g., 14.04.2013

This solution is efficient and readable. The clone keyword ensures that modifying $yesterday does not affect the current date stored in $today. This pattern is particularly useful if you need to calculate a range of dates (e.g., yesterday, today, tomorrow) without recalculating the base timestamp every time.

Working with Legacy PHP and strtotime

If you are maintaining code on older PHP versions or prefer a more concise syntax for simple operations, strtotime remains a viable option. This function converts a date string to a Unix timestamp, allowing for arithmetic operations in seconds (86400 seconds per day).

$tomorrow = time() + 86400;
$yesterday = time() - 86400;

// Format the timestamp directly
$formattedYesterday = date('d.m.Y', $yesterday);

echo $formattedYesterday;

This method is often found in older tutorials and legacy scripts. While it works perfectly fine for standard dates, it lacks the object-oriented clarity of DateTime. Additionally, because date() is a global function, chaining operations can sometimes lead to scope confusion if not managed carefully within complex logic blocks.

Handling Format Variations and Pitfalls

A common pitfall when working with date formats is assuming that simple string subtraction will work. For instance, trying to calculate yesterday by taking the string "15.04.2013", subtracting 1 from the day part to get "14.04.2013", and failing to account for the fact that April has only 30 days is a classic error. If you attempt this logic on January 31st, a naive subtraction would yield February 30th, which does not exist.

By using DateTime or strtotime, PHP automatically handles these transitions:

  • Subtracting one day from March 31st results in March 30th.
  • Subtracting one day from January 1st results in December 31st of the previous year.

If you require high precision or need to format dates for internationalization beyond simple d.m.Y patterns, consider exploring more advanced formatting options available in the IntlDateFormatter extension, though this is rarely necessary for standard server-side logging or display tasks.

For developers looking to integrate these date utilities into larger applications, such as Laravel projects, the framework provides helpers like now() and today(). While not directly related to calculating yesterday, understanding the underlying DateTime mechanics helps when writing custom migrations or generating scheduled task reports. For more in-depth resources on date handling within the ecosystem, you might want to check out LaravelCompany for community-driven insights and best practices.

Our network