In PHP, processing dates and times is one of the very common tasks in daily development. Among them, date_sub is a very practical function that allows us to easily subtract the specified time interval from a DateTime object. This tutorial will explain in detail how to use the date_sub function to subtract a specific time from the current date, such as day, month, year, hour, etc.
date_sub is a built-in function that takes two parameters:
A DateTime object.
A DateInterval object.
The function is to subtract the time interval defined by DateInterval from the DateTime object and return the new DateTime object.
Function prototype:
DateTime date_sub ( DateTime $object , DateInterval $interval )
Note: Starting from PHP 8.0.0, date_sub() has been marked as deprecated. It is recommended to use the DateTime::sub() method to achieve the same function.
First, we need to create a DateTime object representing the current time.
$date = new DateTime(); // Current date and time
Then, create a time interval object. For example, we want to subtract 10 days:
$interval = new DateInterval('P10D'); // P Express period,D Indicates day
The format of DateInterval is ISO 8601, for example:
'P2Y' : 2 years
'P3M' : 3 months
'P15D' : 15 days
'PT5H' : 5 hours
'PT30M' : 30 minutes
It is recommended to use the sub() method of DateTime for subtraction operation:
$date->sub($interval);
We can use the format() method to output the final time:
echo $date->format('Y-m-d H:i:s');
<?php
// Current date and time
$date = new DateTime();
// Create a time interval,For example, subtract 10 sky
$interval = new DateInterval('P10D');
// Subtract this interval from date
$date->sub($interval);
// Output result
echo "10 sky前的日期是:" . $date->format('Y-m-d H:i:s');
?>
For example, your website needs to display a week before the publication date of an article:
$publishDate = new DateTime('2025-05-28');
$interval = new DateInterval('P7D');
$publishDate->sub($interval);
echo $publishDate->format('Y-m-d');
$registerDate = new DateTime();
$interval = new DateInterval('P7D');
$expireDate = clone $registerDate;
$expireDate->add($interval); // Set the invalidation time as the registration time +7sky
echo "Please " . $expireDate->format('Y-m-d H:i:s') . " Previous Activate Account。";
$now = new DateTime();
$interval = new DateInterval('P1M');
$lastMonth = clone $now;
$lastMonth->sub($interval);
// For example, you have a visit statistics page
$url = "https://gitbox.net/logs/view?start=" . $lastMonth->format('Y-m-d') . "&end=" . $now->format('Y-m-d');
echo "View access logs for the past month:<a href='$url'>$url</a>";
Time format error : The format of DateInterval must strictly comply with the ISO 8601 specification and cannot be written randomly.
Object reference problem : Directly modifying the original DateTime object will affect the subsequent logic, and use clone if necessary.
Time zone problem : Inconsistent default time zones may lead to time deviation. It is recommended to manually set the time zone:
date_default_timezone_set('Asia/Shanghai');
Through the combination of DateTime and DateInterval , PHP provides developers with very flexible time computing capabilities. The use of date_sub() or DateTime::sub() methods is relatively simple, but they are powerful and can meet most date subtraction requirements. In daily development, if it involves the functions of judging, statistics, recording, etc. of time range, it will be very helpful to master the use of this function.