Add a duration or interval to a date· 3 minute read
In PHP you can easily add a duration to a
DateTime instance in a number of ways. I will review the most common methods for completing the task starting with those available on the
DateTime object itself.
So if we want to add one day to a date we would use the format
+1 day implemented as so:
<?php $date = new DateTime('2014-01-03'); $date->modify('+1 day'); echo $date->format('Y-m-d'); // 2014-01-04
PHP date and time handling was significantly improved in PHP 5.3+ to include a specific add (and subtract) method to
DateTime. Although instead of simply including a text format you must use a
DateInterval to represent the duration.
In addition the format is slightly different to incorporate the ISO 8601 duration specification. All intervals should begin with a
P (period) and include a integer representation of the interval followed by a period designator such as
D for day.
This would simply be
P1D for our 1 day time period. A more complex example would be
P4Y1M2D, which is 2 days, 1 month and 4 years.
A handy addition to the format specification is the availability of
W for weeks such as
P4Y1W, which works out as 1 week and 4 years. You should note that
W cannot be combined with days because
W units get converted into days.
You can follow this with a
T if you wish to include a time period. Just as with the date you include an integer for the interval period followed by a period designator.
Should you want to make our period 1 day and 2 hours then the format would be
P1DT2H. To make it more interesting here is a full format
P4Y1M2DT1H2M3S that works out as 2 days, 1 month, 4 years and 1 hour, 2 minutes and 2 seconds.
Of course it is also possible to only specify the time portion of the interval. This allows you to add periods of an hour or even seconds to a
DateTime instance. Logically this is specified as
PT1H2M3S for a interval of 1 hour, 2 minutes and 2 seconds.
One final note on the formatting; you must specify the units in descending order where you are using more than one period designator. Essentially this means that larger units such as years (
Y) should come before smaller units like seconds (
With this in mind we can see that
P4Y1D are valid formats whereas
P1D4Y are not.
It is worth pointing out here that you can use the same relative formats from the the
DateTime::modify() method if you prefer using the
DateInterval::createFromDateString() static method (see PHP documentation).
So back to the original example of adding one day to our
<?php $date = new DateTime('2014-01-03'); $date->add(new DateInterval('P1D')); echo $date->format('Y-m-d'); // 2014-01-04
You can also subtract intervals from
DateTime instances as well:
<?php $date = new DateTime('2014-01-03'); $date->sub(new DateInterval('P1D')); echo $date->format('Y-m-d'); // 2014-01-02
As you can see PHP provides a rich API for dealing with dates and times and their modification via simple instructions. It is also possible to adjust a
DateTime instances time zone as I have previously blogged in Convert UTC/GMT or any time zone to local time in PHP.
Based on this I would point out that if you are still using the older procedural API for your PHP date operations then you are missing out on a lot of the power afforded you by the languages standard library.
For a final tidbit I would recommend a little further reading about the
DateInterval::format() method, which can also be handy when debugging intervals.