• PHP Video Tutorials

PHP date_modify() Function


Advertisements

Definition and Usage

The date_modify() function is an alias of DateTime::modify(). This function is used to modify the date in a DateTime object. It alters the time stamp of the given object.

Syntax

date_modify($object, $modify)

Parameters

Sr.No Parameter & Description
1

object (Mandatory)

This represents the DateTime object you want to modify.

2

modify (Mandatory)

This is a date/time string specifying the modification needed.

Return Values

PHP date_modify() function returns the DateTime object with modified value. Incase of failure, this function returns the boolean value false.

PHP Version

This function was first introduced in PHP Version 5.2.0 and, works with all the later versions.

Example

Following example demonstrates the usage of the date_modify() function −

Live Demo
<?php
   //Modifying the date
   $date = date_modify(new DateTime(), "+15 day");   
   print("Date: ".date_format($date, "Y/m/d"));
?>

This will produce following result −

Date: 2020/05/21

Example

Following example creates a DateTime object and modifies its date using the date_modify() function. −

<?php
   //Creating a DateTime object
   $date_time_Obj = date_create("25-09-1989");
   print("Original Date: ".date_format($date_time_Obj, "Y/m/d"));
   print("\n");
   //Setting the date
   $date = date_modify($date_time_Obj, "+15 years 7 months 23 days" );   
   print("Modified Date: ".date_format($date, "Y/m/d"));
?>

This will produce following result −

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

Example

You can also modify a date by specifying the number of weeks as −

<?php
   //Creating a DateTime object
   $date_time_Obj = date_create("25-09-1989");
   print("Original Date: ".date_format($date_time_Obj, "Y/m/d"));
   print("\n");
   //Setting the date
   $date = date_modify($date_time_Obj, "1960 weeks" );   
   print("Modified Date: ".date_format($date, "Y/m/d"));
?>

This will produce the following output −

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

Example

Live Demo
<?php
   $date = new DateTime("1990-12-12");
   $date->modify("+1 day");
   
   echo $date->format("Y-m-d");
?>

This will produce the following output −

1990-12-13
php_function_reference.htm
Advertisements