How to subtract days from a date in php?

A one-liner option is:

echo date_create('2011-04-24')->modify('-1 days')->format('Y-m-d');

Running it on Online PHP Editor.


mktime alternative

If you prefer to avoid using string methods, or going into calculations, or even creating additional variables, mktime supports subtraction and negative values in the following way:

// Today's date
echo date('Y-m-d'); // 2016-03-22

// Yesterday's date
echo date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")-1, date("Y"))); // 2016-03-21

// 42 days ago
echo date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")-42, date("Y"))); // 2016-02-09

//Using a previous date object
$date_object = new DateTime('2011-04-24');
echo date('Y-m-d',
  mktime(0, 0, 0,
     $date_object->format("m"),
     $date_object->format("d")-1,
     $date_object->format("Y")
    )
); // 2011-04-23

Online PHP Editor

❮ PHP Date/Time Reference

Example

Subtract 40 days from the 15th of March, 2013:

$date=date_create("2013-03-15");
date_sub($date,date_interval_create_from_date_string("40 days"));
echo date_format($date,"Y-m-d");
?>

Try it Yourself »


Definition and Usage

The date_sub() function subtracts some days, months, years, hours, minutes, and seconds from a date.


Syntax

date_sub(object, interval)

Parameter Values

ParameterDescription
object Required. Specifies a DateTime object returned by date_create()
interval Required. Specifies a DateInterval object

Technical Details

Return Value:Returns a DateTime object on success. FALSE on failure
PHP Version:5.3+

❮ PHP Date/Time Reference


  1. HowTo
  2. PHP Howtos
  3. Subtract Days With PHP

Created: December-30, 2021 | Updated: March-29, 2022

  1. Using the strtotime() Method to Subtract Days in PHP
  2. Using the DateTime() to Subtract Days in PHP
  3. Subtract From the Given Date in PHP
  4. Subtract Week From the Given Date in PHP

It is an important method for subtracting days, weeks, or months from a given date in PHP. The ways like we may use either PHP’s strtotime method or the in-built DateTime class to do.

The date() and strtotime() both function are use in PHP. Which makes it simple to subtract time (hours, minutes, and seconds) from a given date or current time (date).

The date() method returns a prepared string after formatting a specific time.

On the other side turns a text formatted DateTime into a Unix timestamp. date() and strtotime() can help subtract time from the current time (date) in PHP.

So here is the way to subtract time from a current DateTime. Subtract 1 day with PHP form current date:

Using the strtotime() Method to Subtract Days in PHP

Sample Code:


Output:

2021-12-06
2021-12-05

Above is an example of subtracting days from the current date by providing the string yesterday to strtotime.

Using the DateTime() to Subtract Days in PHP

Sample Code:

sub(new DateInterval('P1D'));
 
//Get yesterday date
$yesterday = $yesterdayTime->format('Y-m-d');
 
//Print yesterday date.
echo $yesterday;
?>

Output:

2021-12-05

We discussed PHP’s old version 5.3.0, using the DateInterval class. And it represents a date period.

Now, we will discuss P1D. We define the DateInterval class’s objects to P1D, which means one day (one-day period).

The interval can deduct from the given date and time. We can use P5D (five days) instead of P1D (one day) if you want to remove five days instead of one.

Subtract From the Given Date in PHP

Sample Code:

sub(new DateInterval('P1D'));
 
//Get the date in a YYYY-MM-DD format.
$yesterday = $yesterdayTime->format('Y-m-d');
//Print Date.
echo $yesterday;
?>

Output:

2020-12-31

Subtract Week From the Given Date in PHP

Using strtotime()

Sample Code:


Output:

2021-11-29

As we know that from the start time() method, we can subtract time, day, month, and year from a given date.

The $interval spec argument DateInterval class for P1W. Which stands for a time of one week, P1W=one week Period. Now, If you want to Change P1W (one week period) to P2W to deduct two weeks, it will be a great approach.

Related Article - PHP DateTime

  • Get the Last Day of the Month With PHP Functions
  • Calculate the Difference Between Two Dates Using PHP
  • Convert DateTime to String in PHP
  • Get the Current Year in PHP
  • How can I get the difference between two dates in PHP?

    To calculate the difference between two dates in PHP, call date_diff() date/time function, and pass the two dates as argument to it. date_diff() function returns a DateInterval object, or FALSE if calculating the difference is not successful.

    How do I subtract days from a date in Excel?

    Add days to or subtract days from a date.
    Open a new sheet in a workbook..
    In cell A1, type 2/8/12..
    In cell B1, type =A1-15, and then press RETURN . This formula subtracts 15 days from the date in cell A1..
    In cell C1, type =A1+30, and then press RETURN . ... .
    In cell D1, type =C1-15, and then press RETURN ..

    How can I get yesterday date in PHP?

    Using strtotime() function In PHP strtotime() is built-in function and we can make use of it for finding yesterday's date in PHP, this function simply converts English textual date and time into a Unix time stamp. We can simply do this by passing yesterday's as a parameter in function or passing -1 days in function.