modify() : Date modify

This is another way to add or subtract a date object with different month , days ,year , hour , minutes , sec etc.
This function is supported by PHP 5.2 or higher versions. So if you are running 5.2 version the you can use modify function as add or subtract functions are supported by PHP 5.3 and higher only.
How to know PHP version ?
Here is an example on how to use modify function.
<?Php
date_default_timezone_set ("Asia/Kolkata");
$date1=new DateTime("now") ; // current date
echo $date1->format('d-m-Y');
echo "<br>";
$date2=$date1->modify('+5 day');
 echo $date2->format('d-m-Y');
?>
The output is here
17-10-2025
22-10-2025
We can change the highlighted line like this
$date2=$date1->modify('+3 month +5 day -1 year ');
Output of this change is here
17-10-2025
22-01-2025

Including Time

<?Php
date_default_timezone_set ("Asia/Kolkata");
$date1=new DateTime("now") ;
echo $date1->format('d-m-Y H:i:s');
echo "<br>";
$date2=$date1->modify('+5 day +6 hours +20 minutes +30 seconds ');
echo $date2->format('d-m-Y H:i:s');
?>
Output is here ( based on today's date and time). Refresh this page and check the changes in time.
17-10-2025 20:01:12
23-10-2025 02:21:42

Using fixed date and leap year

$date = new DateTime('2024-02-29 12:00:00');
$date2=$date->modify('+1 year +5 hours');
echo $date2->format('d-m-Y H:i:s');
Output is here
01-03-2025 17:00:00
PHP Date Functions Timestamp in PHP Displaying calendar for selection of date by user
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer