PHP date_sub() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Date/Time

Definition

In PHP, date_sub() subtracts a DateInterval (such as 10 days or 2 hours) from a DateTime object.

It is the procedural name for DateTime::sub() (and DateTimeImmutable::sub()). The DateTime object is changed in place and also returned.

Syntax

Call date_sub() with the values you need. Start with this line.

PHP
date_sub($date, new DateInterval("P10D"));
CallWhat it does
date_sub($date, new DateInterval("P10D"))Moves the date 10 days back.
date_sub($date, new DateInterval("P1M"))Moves the date 1 month back.
date_sub($date, new DateInterval("PT2H"))Moves the time 2 hours back.

The interval uses ISO 8601 text: P is for the date part, T separates the time part, and letters show the unit (D days, M months, H hours). To avoid changing the original, use DateTimeImmutable::sub(), which returns a new object.

Library declarations
PHP
date_sub(DateTime $object, DateInterval $interval): DateTime

Parameters

ParameterDescription
objectRequired. The DateTime object to change.
intervalRequired. A DateInterval object that says how much to subtract.

Return Value

Return ValueDescription
DateTimeThe same DateTime object after the interval has been subtracted.

PHP Examples

Example 1 — Subtract Days, Months, and Hours

Each call changes the same object, so each result builds on the one before it.

PHP
<?php
$date = date_create("2024-03-15 09:30:00");

date_sub($date, new DateInterval("P10D"));
echo date_format($date, "Y-m-d H:i"), PHP_EOL;

date_sub($date, new DateInterval("P1M"));
echo date_format($date, "Y-m-d H:i"), PHP_EOL;

date_sub($date, new DateInterval("PT2H"));
echo date_format($date, "Y-m-d H:i"), PHP_EOL;

How It Works

  1. Start at March 15, 2024, 09:30.
  2. P10D takes away 10 days, which gives March 5.
  3. P1M takes away one month, which gives February 5.
  4. PT2H takes away 2 hours, so the time becomes 07:30.

Example 2 — Month-End Surprise

Subtracting a month from March 31 rolls over, because February 31 does not exist.

PHP
<?php
$a = date_create("2024-03-31");
date_sub($a, new DateInterval("P1M"));
echo date_format($a, "Y-m-d"), PHP_EOL;

$b = date_create("2024-03-31");
date_modify($b, "last day of previous month");
echo date_format($b, "Y-m-d"), PHP_EOL;

How It Works

  1. March 31 minus 1 month gives February 31, a date that does not exist.
  2. PHP rolls the extra days into March, so the result is March 2.
  3. date_modify() with last day of previous month always picks a real date.
  4. In the leap year 2024, that date is February 29.

Example 3 — Subtract with DateTimeImmutable

The modern approach keeps the original date and gives you a new one.

PHP
<?php
$start = new DateTimeImmutable("2024-12-25");
$interval = date_interval_create_from_date_string("2 weeks");
$earlier = $start->sub($interval);

echo "Start: ", $start->format("Y-m-d"), PHP_EOL;
echo "Earlier: ", $earlier->format("Y-m-d"), PHP_EOL;

How It Works

  1. Create a DateTimeImmutable for December 25, 2024.
  2. date_interval_create_from_date_string() builds an interval of 2 weeks (14 days).
  3. sub() returns a new object, so $start is not changed.
  4. Fourteen days before December 25 is December 11.

Frequently Asked Questions

It subtracts a DateInterval from a DateTime object. For example, subtracting P10D moves the date back by 10 days.
Yes. With DateTime, the object you pass in is changed. DateTimeImmutable::sub() is the safer option because it returns a new object and keeps the original.
Use new DateInterval() with an ISO 8601 text such as P10D (10 days), P1M (1 month), or PT2H (2 hours). You can also use date_interval_create_from_date_string("10 days").
If the earlier month is shorter, the extra days roll forward. March 31 minus one month becomes March 2 in 2024. Use "last day of previous month" when you need a real month end.
Yes. It is the procedural alias of DateTime::sub() and behaves the same way.

Next: date_sun_info()

date_sun_info() returns an array with the sunrise, sunset, solar noon, and twilight times .

date_sun_info() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful