php

How to Use date_modify() Function in PHP

The date_modify() is a built-in PHP function used to modify the date/time value of a date object. This function is a part of the DateTime class and is used to add or subtract a specified interval from a date object. Date objects are created using the DateTime class in PHP. The date_modify() function is then used to modify these objects by adding or subtracting a specified time interval.

Syntax for date_modify() Function in PHP

The syntax of the date_modify() function in PHP is given below.

date_modify (DateTime $object, string $modify)

It takes two parameters:

  • $object: A “DateTime” object that you want to modify.
  • $modify: A string representing the modifications you want to the date/time or the object.

Return Value

The date_modify() function returns the modified DateTime object.

How to Use date_modify() Function in PHP

The following are some of the uses of the date_modify() function in PHP.

1: Basic Usage

The basic usage of the date_modify() function involves passing the DateTime object and a string specifying the modifications.

<?php

$date = new DateTime("2023-03-12");

echo $date->format("Y-m-d") . "\n";

date_modify($date, "+3 days");

echo $date->format("Y-m-d") . "\n";

?>

The code above generates a DateTime object with the date 2023-03-12 as its first value. The modified date is then printed using the “Y-m-d” format after being adjusted using the date_modify() function to add 3 days. The new date is then printed using the date->format function.

2: Combining Multiple Modifications

The date_modify() function allows for combining multiple modifications in a single operation. Suppose we want to subtract 1 month and 10 days from a given date.

<?php

$date = new DateTime("2023-03-12");

date_modify($date, "-2 month -3 days");

echo $date->format("Y-m-d");

?>

In the above code, we generate a DateTime object representing the date 2023-03-12. We then use the date_modify() function with the modifications “-2 month -3 days” to subtract 2 months and 3 days from the date.

3: Setting a Specific Date and Time

We can also use the date_modify() function to set a specific date and time value for the date object. For example, if we want to set the date to January 1, 2022, we can do it in the following way:

<?php

$date = new DateTime("2023-06-15");

date_modify($date, "2022-01-01");

echo $date->format("Y-m-d\n");

?>

The date 2023-06-15 is represented by a DateTime object created by this code. The modification string “2022-01-01” is then used to attempt to change the date. The original date “2023-06-15” is then formatted and printed.

This sets the date of the date object to January 1, 2022.

We can also set a specific time value for the date object using the date_modify() function. For example, if we want to set the time to 12:30 PM, we can do it in the following way:

<?php

$date = new DateTime("2023-06-15");

date_modify($date, "12:30 PM");

echo $date->format("Y-m-d H:i:s");

?>

This sets the time of the date object to 12:30 PM.

4: Creating Specific Formats

Another useful application of the date_modify() function is in creating specific date/time formats. We can use this function to format the date object in any format we want. For example, if we want to format the date object as “YYYY-MM-DD”, we can do it as follows:

<?php

$date = new DateTime("2023-06-15");

date_modify($date,"12-02-2022");

echo $date->format("d-m-Y");

?>

This will format the date object as 12-02-2022. Similarly, we can format the date object in any format we want by specifying the appropriate format string in the format() function.

Conclusion

The date_modify() function in PHP is used to alter the date/time value of a date object. It can be used to set a specific date and time, combine multiple modifications in a function, or create a specific format for the date or time in a function. This guide demonstrated various use cases of the date_modify() function that helps users understand the working of this function.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.