how to get previous day in PHP

how to get previous day in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Subtracting One Day from the Current Date in PHP

When working with dates and times in PHP, the most common starting point is retrieving the current date using date('Y/m/d'). This function formats the system's current timestamp into a human-readable string. However, simply echoing that result doesn't help if you need to perform temporal arithmetic, such as calculating yesterday's date. The goal is often to take today's date and subtract exactly 24 hours (or one calendar day) without worrying about the current time of day.

The solution relies on manipulating the timestamp representation of a date. A Unix timestamp represents a specific moment in time as the number of seconds elapsed since January 1, 1970. To get yesterday's date, you don't necessarily need to convert the result back to a string immediately; instead, you can adjust the timestamp by subtracting the number of seconds in a day (86,400) and then format that adjusted time.

The most straightforward approach uses the strtotime function combined with arithmetic or the DateTime class. If you are using the date() function directly on a modified string, you might see code like this:

$yesterday = date('Y-m-d', strtotime('-1 day'));
echo $yesterday; // Output: 2015-08-09 (assuming today is August 10)

Here, strtotime('-1 day') parses the string relative to the current time and returns a new timestamp. Subtracting one day from the current timestamp effectively moves the pointer back 24 hours. When you pass this adjusted timestamp to date('Y-m-d'), it strips away the hour, minute, second, and timezone information, leaving only the calendar date of yesterday.

Alternatively, if you prefer a more explicit approach that handles timezones automatically, the DateTime class is often preferred for complex scenarios. You can create an instance of DateTime, modify it, and then format it:

$today = new DateTime();
$yesterday = clone $today;
$yesterday->modify('-1 day');
echo $yesterday->format('Y-m-d');

This method is particularly robust because the DateTime object holds timezone information. If you simply subtract seconds from a raw timestamp without considering the timezone, you might inadvertently cross midnight in a way that doesn't match your server's local expectation. The modify() method handles the calendar logic internally, ensuring that if today is August 10th, the result is strictly August 9th, regardless of whether it is currently 3:00 AM or 11:59 PM.

A common pitfall arises when developers confuse the output of date('Y-m-d', strtotime('-1 day')) with a Unix timestamp. The expression UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY)) mentioned in the source context is valid SQL syntax (often used with MySQL), but it cannot be executed directly in PHP code as written. PHP does not have a built-in DATE_SUB function, nor does it execute raw SQL keywords like NOW() without wrapping them in a database connection context. In pure PHP, you must stick to the native date, strtotime, or DateTime functions to achieve this logic.

If you are building an application that requires frequent date manipulation, consider whether a dedicated package simplifies your workflow. For instance, the Carbon library provides an intuitive interface for these operations:

use Carbon\Carbon;
$yesterday = Carbon::now()->subDay();
echo $yesterday->format('Y-m-d');

While adding a dependency introduces overhead, it significantly reduces syntax errors and handles edge cases like month rollovers or daylight saving time transitions more gracefully than native functions. If your project is already built on the Laravel framework, you might find that integrating such tools aligns well with the ecosystem's conventions. For more resources on date handling within the Laravel context, you can explore discussions at https://laravelcompany.com.

Ultimately, the choice between date() and DateTime depends on whether you need to perform further calculations or just format a single string. Using DateTime is generally recommended for production code because it separates the concept of "now" from the formatting logic, making your codebase more maintainable and less prone to timezone-related bugs.

Our network