Home PHP Date/Time date_modify() PHP date_modify() Function Definition In PHP, date_modify() changes a DateTime object using a relative text such as "+1 day", "next friday", or "last day of this month".
It is the procedural name for DateTime::modify() (and DateTimeImmutable::modify()). With DateTime the object itself changes; with DateTimeImmutable you get a new object.
Syntax Call date_modify() with the values you need. Start with this line.
date_modify($date, "+1 week"); Call What it does date_modify($date, "+1 week")Moves the date seven days forward. date_modify($date, "next friday")Moves to the next Friday after the current date. date_modify($date, "last day of this month")Moves to the last day of the current month.
Because date_modify() changes the object you pass in, most modern code uses DateTimeImmutable::modify(), which leaves the original alone and returns a new object. If the modifier text is invalid, the function returns false and PHP shows a warning.
Library declarations date_modify(DateTime $object, string $modifier): DateTime|false Parameters Parameter Description object Required. The DateTime object to change. modifier Required. A relative date or time text such as "+3 days", "-1 month", or "tomorrow".
Return Value Return Value Description DateTime The same object after it has been changed. false Returned (with a warning) when the modifier text cannot be parsed.
PHP Examples 1. Move a Date Step by Step 2. End of Month and Invalid Text 3. DateTime vs DateTimeImmutable Example 1 — Move a Date Step by Step Each call changes the same object, so the changes build on each other.
<?php
$date = date_create("2024-03-10");
date_modify($date, "+1 week");
echo date_format($date, "Y-m-d"), PHP_EOL;
date_modify($date, "next friday");
echo date_format($date, "Y-m-d"), PHP_EOL;
date_modify($date, "last day of this month");
echo date_format($date, "Y-m-d"), PHP_EOL; 2024-03-17
2024-03-22
2024-03-31 How It Works Start on Sunday, March 10, 2024. +1 week moves the date to March 17. next friday moves from Sunday, March 17 to Friday, March 22. last day of this month moves to March 31. Example 2 — End of Month and Invalid Text Adding a month at the end of January rolls over. A special phrase fixes that, and bad text gives false.
<?php
$a = date_create("2024-01-31");
date_modify($a, "+1 month");
echo date_format($a, "Y-m-d"), PHP_EOL;
$b = date_create("2024-01-31");
date_modify($b, "last day of next month");
echo date_format($b, "Y-m-d"), PHP_EOL;
var_dump(@date_modify($b, "banana")); 2024-03-02
2024-02-29
bool(false) How It Works +1 month on January 31 gives February 31, which does not exist, so PHP rolls over to March 2. last day of next month always lands on a real date, here February 29 (2024 is a leap year). "banana" is not a valid modifier, so date_modify() returns false. The @ sign hides the warning for this demo. In real code, check for false instead.Frequently Asked Questions What does date_modify() do in PHP? It changes a DateTime object using a relative text such as "+1 day" or "next monday". It returns the changed object, or false if the text is invalid.
Does date_modify() change the original object? Yes, for DateTime it does. If you do not want that, use DateTimeImmutable::modify(), which returns a new object and leaves the original unchanged.
How do I get the last day of a month with date_modify()? Use "last day of this month" or "last day of next month". These phrases always give a real date, even when adding a plain month would roll over.
What does date_modify() return for invalid text? It returns false and PHP shows a warning. Always check the return value when the modifier comes from user input.
Is date_modify() the same as DateTime::modify()? Yes. It is the procedural alias of DateTime::modify(), and the same modifier texts work for both.
Next: date_offset_get() date_offset_get() returns the timezone offset of a date object in seconds, including dayli.
date_offset_get() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion