How to Get Yesterday's Date in PHP

Shraddha Paghdar Feb 02, 2024
  1. date() in PHP
  2. DateInterval in PHP
  3. Using strtotime() to Get Yesterday’s Date in PHP
  4. Using mktime() to Get Yesterday’s Date in PHP
  5. Using time() to Get Yesterday’s Date in PHP
  6. Using DateInterval to Get Yesterday’s Date in PHP
How to Get Yesterday's Date in PHP

This article will introduce how to get yesterday’s date in PHP.

Before learning the solution, let’s understand the concept of date().

date() in PHP

It is a built-in PHP function that returns the formatted date string.

Syntax of date()

date(format, timestamp)

Parameters

format: This is a mandatory parameter that specifies the output date string format. Some of the options are:

  1. d - The day of the month in the range of 01 to 31
  2. D - A text representation of a day (three letters)
  3. m - A numeric representation of a month in the range of 01 to 12
  4. M - A text representation of a month (three letters)
  5. Y - A four-digit representation of a year
  6. y - A two-digit representation of a year
  7. a - Lowercase am or pm
  8. A - Uppercase AM or PM

timestamp: It is an optional parameter that specifies a Unix timestamp in integer format. If not provided, a default value will be taken as the current local time.

DateInterval in PHP

It is a Class of PHP that represents a date interval. It also provides the static method, which accepts the input strings and sets up a DateInterval from the input string.

Now that we have understood the basic concept of date() , strtotime() and mktime(). We’ll use all of these functions to get the date of yesterday.

Using strtotime() to Get Yesterday’s Date in PHP

strtotime() is a built-in PHP function parses an English textual DateTime into a Unix timestamp from January 1, 1970, 00:00:00 GMT.

Syntax of strtotime()

strtotime(time, now);

Parameter

  • time: This is a mandatory parameter, which specifies a date/time string.
  • now: This is an optional parameter, specifying the timestamp used as the basis for calculating relative dates.

We can pass either yesterday or -1 days to the strtotime function to get yesterday’s timestamp. As introduced above, the timestamp can be converted to the date in the string format by the date() function.

Example code:

<?php
    // Get yesterdays date
    echo date('d.m.Y',strtotime("-1 days")). "\n";
    echo date('d M Y',strtotime("yesterday")); 
?>

Output:

24.10.2021
24 Oct 2021

Using mktime() to Get Yesterday’s Date in PHP

It is a built-in PHP function that returns the Unix timestamp for a date. This function is almost the same as gmmktime() except the passed parameters represent a date (not a GMT date).

Syntax

mktime(hour, minute, second, month, day, year)

Parameter

  • hour: This is an optional parameter, which specifies the hour.
  • minute: It is an optional parameter, which specifies the minute.
  • second: It is an optional parameter, which specifies the second.
  • month: It is an optional parameter, which specifies the month.
  • day: It is an optional parameter, which specifies the day.
  • year: It is an optional parameter, which specifies the year.

Example code:

<?php
    $m = date("m"); // Month value
    $de = date("d"); // Today's date
    $y = date("Y"); // Year value

    echo "Yesterday's date was: " . date('d-m-Y', mktime(0,0,0,$m,($de-1),$y)); 
?>

Output:

Yesterday's date was: 24-10-2021

The values of year and month are the same between today and yesterday. The day value of yesterday is one less than that of today.

Using time() to Get Yesterday’s Date in PHP

The time() function returns the current timestamp. If we subtract its value, then we get the timestamp of the same time yesterday.

Example code:

<?php
    echo date('d M Y', time() - 60 * 60 * 24);
?>

Output:

24 Oct 2021

Using DateInterval to Get Yesterday’s Date in PHP

It is a Class of PHP that represents a date interval. It also provides the static method, which accepts the input strings and sets up a DateInterval from the input string.

Syntax of DateInterval()

new DateInterval($period);

Parameter

$period: It is a mandatory parameter that specifies the time in the form of a string. For example, P1D specifies 1 day. PT1H specifies 1 hour.

  1. P$numberD - A time in the form of day. $number is in the range of 1-31.
  2. P$numberM - A time in the form of the month. $number is in the range of 1-12.
  3. P$numberY - A Time in the form of the year. $number is in the range of 1-100.
  4. PT$numberH - A Time in the form of an hour. $number is in the range of 1-24.
  5. PT$numberM - A Time in the form of a minute. $number is in the range of 0-60.
  6. PT$numberS - A Time in the form of the second. $number is in the range of 0-60.

Syntax of DateInterval::createFromDateString()

public static DateInterval::createFromDateString(string $datetime);

Parameter

$datetime: It is a mandatory parameter that specifies the date/time in string format.

We can pass yesterday to the createFromDateString() and P1D to the DateInterval function to get yesterday’s timestamp. We can add or subtract this timestamp from the current timestamp, and the resulted timestamp can be converted to the date in the string format by the date() function.

Example code:

<?php
    $date = new DateTime();
    $date->add(DateInterval::createFromDateString('yesterday'));
    echo $date->format('d M Y') . "\n";

    $date = new DateTime();
    $date->sub(new DateInterval('P1D'));
    echo $date->format('d M Y') . "\n";
?>

Output:

24 Oct 2021
24 Oct 2021
Shraddha Paghdar avatar Shraddha Paghdar avatar

Shraddha is a JavaScript nerd that utilises it for everything from experimenting to assisting individuals and businesses with day-to-day operations and business growth. She is a writer, chef, and computer programmer. As a senior MEAN/MERN stack developer and project manager with more than 4 years of experience in this sector, she now handles multiple projects. She has been producing technical writing for at least a year and a half. She enjoys coming up with fresh, innovative ideas.

LinkedIn

Related Article - PHP Date