PHP date_modify() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Date/Time

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.

PHP
date_modify($date, "+1 week");
CallWhat 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
PHP
date_modify(DateTime $object, string $modifier): DateTime|false

Parameters

ParameterDescription
objectRequired. The DateTime object to change.
modifierRequired. A relative date or time text such as "+3 days", "-1 month", or "tomorrow".

Return Value

Return ValueDescription
DateTimeThe same object after it has been changed.
falseReturned (with a warning) when the modifier text cannot be parsed.

PHP Examples

Example 1 — Move a Date Step by Step

Each call changes the same object, so the changes build on each other.

PHP
<?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;

How It Works

  1. Start on Sunday, March 10, 2024.
  2. +1 week moves the date to March 17.
  3. next friday moves from Sunday, March 17 to Friday, March 22.
  4. 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
<?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"));

How It Works

  1. +1 month on January 31 gives February 31, which does not exist, so PHP rolls over to March 2.
  2. last day of next month always lands on a real date, here February 29 (2024 is a leap year).
  3. "banana" is not a valid modifier, so date_modify() returns false.
  4. The @ sign hides the warning for this demo. In real code, check for false instead.

Example 3 — DateTime vs DateTimeImmutable

date_modify() changes the object itself. DateTimeImmutable::modify() returns a new one.

PHP
<?php
$mutable = date_create("2024-05-01");
date_modify($mutable, "+10 days");
echo "Mutable: ", date_format($mutable, "Y-m-d"), PHP_EOL;

$original = new DateTimeImmutable("2024-05-01");
$later = $original->modify("+10 days");
echo "Original: ", $original->format("Y-m-d"), PHP_EOL;
echo "Later: ", $later->format("Y-m-d"), PHP_EOL;

How It Works

  1. date_modify() adds 10 days directly to $mutable, so it now holds May 11.
  2. Create a DateTimeImmutable for May 1.
  3. modify() returns a new object and stores it in $later.
  4. The original is still May 1, which makes immutable dates safer to share.

Frequently Asked Questions

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.
Yes, for DateTime it does. If you do not want that, use DateTimeImmutable::modify(), which returns a new object and leaves the original unchanged.
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.
It returns false and PHP shows a warning. Always check the return value when the modifier comes from user input.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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