The date_modify Function
The date_modify
function allows adding
and subtracting specific time intervals from
a date. The date must be an object
created by the date_create
function.
The function modifies the passed object itself, and
also returns the modified date object.
Syntax
date_modify(DateTime $object, string $modifier): DateTime|false
Example
Let's create a date object for the year 2025
,
month 12
, day 31
, then
add 1
day to it and output it in the
format 'day.month.year'
:
<?php
$date = date_create('2025-12-31');
date_modify($date, '1 day');
echo date_format($date, 'd.m.Y');
?>
Code execution result:
'01.01.2026'
Example
Let's create a date object for the year 2025
,
month 12
, day 31
, then
add 3
days to it and output it in the format
'day.month.year'
:
<?php
$date = date_create('2025-12-31');
date_modify($date, '3 days');
echo date_format($date, 'd.m.Y');
?>
Code execution result:
'03.01.2026'
Example
Let's create a date object for the year 2025
,
month 12
, day 31
, then
add 3
days and 1
month to it
and output it in the format 'day.month.year'
:
<?php
$date = date_create('2025-12-31');
date_modify($date, '3 days 1 month');
echo date_format($date, 'd.m.Y');
?>
Code execution result:
'03.02.2026'
Example
Let's create a date object for the year 2025
,
month 1
, day 1
, then
subtract 1
day from it and output it in the
format 'day.month.year'
:
<?php
$date = date_create('2025-01-01');
date_modify($date, '-1 day');
echo date_format($date, 'd.m.Y');
?>
Code execution result:
'31.12.2024'
See Also
-
the
date_format
function,
which formats a date