We now have a youtube channel. Subscribe!

PHP | date_modify() Function

PHP date_modify() Function



Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP date_modify() Function.

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

Syntax

Following below is the syntax to use this function -

date_modify($object, $modify)


Parameter Details

Sr.NoParameter & 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 Value

This function returns the DateTime object with modified value. Incase of failure, this PHP function returns FALSE.

PHP Version

This function was first introduced as part of core PHP v 5.2.0 and, works with all the later versions.

Example1

Following example illustrates the usage of the PHP date_modify() function -

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

Output

When the above code is executed, it will produce the following result -

Date: 2020/05/21

Example2

The following example creates a DateTime object and modifies its date using the PHP 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"));
?>

Output

When the above code is executed, it will produce the following result -

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

Example3

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"));
?>

Output

When the above code is executed, it will produce the following result -

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

Example4

Try the following example -

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

Output

When the above code is executed, it will produce the following result -

1990-12-13


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the PHP date_offset_get() Function.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain