English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

PHP date_modify() Function Usage and Example

PHP Date & Time Function Manual

The date_modify() function modifies the value of the date-time (DateTime) object

Definition and Usage

The date_modify() function is an alias of DateTime::modify(). This function is used to modify the date in the DateTime object. It changes the timestamp of the given object.

Syntax

date_modify($object, $modify)

Parameter

NumberParameters and Description
1

object (required)

This indicates the DateTime object you want to modify.

2

modify (required)

This is the date/Time string, specifying the required modification.

Return Value

 Returns the modified DateTime object. If it fails, this function will return a boolean valuefalse.

PHP Version

This function was originally introduced in PHP version5.2introduced in version 5.2.0 and is available in all higher versions.

Online Example

The following examples demonstratedate_modify()Function Usage-

<?php
   //Modify Date
   $date = date_modify(new DateTime(), "+15 day);   
   print("Date: ".date_format($date, "Y/m/d"));
?>
Test and see‹/›

Output Result

Date: 2020/05/21

Online Example

The following example creates a DateTime object and usesdate_modify()function to modify its date.-

<?php
   //Create a DateTime object
   $date_time_Obj = date_create("25-09-1989");
   print("Original Date: ".date_format($date_time_Obj, "Y/m/d"));
   print("\n");
   //Set Date
   $date = date_modify($date_time_Obj, "+15 years 7 months 23 days");   
   print("Modify Date: ".date_format($date, "Y/m/d"));
?>
Test and see‹/›

Output Result

Original Date: 1989/09/25
Modify Date: 2005/05/18

Online Example

You can also modify the date by specifying the number of weeks, such as

<?php
   //Create DateTime object
   $date_time_Obj = date_create("25-09-1989");
   print("Original Date: ".date_format($date_time_Obj, "Y/m/d"));
   print("\n");
   //Set Date
   $date = date_modify($date_time_Obj, "1960 weeks");   
   print("Modify Date: ".date_format($date, "Y/m/d"));
?>
Test and see‹/›

This will produce the following output-

Original Date: 1989/09/25
Modify Date: 2027/04/19

Online Example

Add the specified date by1day

<?php
   $date = new DateTime("1990-12-12");
   $date->modify("+1 day);
   
   echo $date->format("Y-m-d());
?>
Test and see‹/›

This will produce the following output-

1990-12-13