PHP has a lot of methods and functions to help you manipulate dates in a variety of ways. In this quick tip, I will show you how to add days to a date in PHP using the DateInterval
class. We will begin with an overview of DateInterval
and then move on to actual addition.
The DateInterval
Class
The DateInterval
class is useful for storing a specific amount of time mentioned in terms of years, months, hours etc. The constructor for this class accepts the specified interval as a string which starts with P. This P stands for the period. You will also have to use the letter T for intervals which contain a time component i.e., hours, minutes and seconds.
The following table lists all the characters used to designate specific periods.
Character | What it Represents |
---|---|
Y | years |
M | months |
W | weeks (converted into days) |
D | days |
H | hours |
M | minutes |
S | seconds |
You might have noticed that M represents both months and minutes but the ambiguity is resolved by the character T that I mentioned earlier. Since minutes represent the time component of our interval, they will be preceded by a T. Here are some examples to make things clear.
String | Time Interval |
---|---|
P20D | 20 days |
P5Y | 5 years |
P2Y3M | 2 years and 3 months |
PT10M | 10 minutes |
P2Y3MT1H10M | 2 years, 3 months, 1 hour and 10 minutes |
There are two things that you have to remember when specifying the duration.
- You cannot combine weeks with days in PHP versions before 8.0.0. P1W3D will be interpreted as 10 days from PHP 8.0.0 onward but as 3 days in earlier versions.
- You have to move from largest to smallest units of time when specifying the duration. This means that years will always have to come before months, months before days and so on.
Adding Days to a Date with PHP DateInterval
Now that we now how to specify date intervals, we will learn how to add days to a date in PHP. We can use the add()
method of the DateTime
class. Here is an example:
<?php $date = new DateTime('2021-12-15'); echo $date->format('M d, Y H:i:s'); // Dec 15, 2021 00:00:00 $interval = new DateInterval('P18DT6M'); $date->add($interval); echo $date->format('M d, Y H:i:s'); // Jan 02, 2022 00:06:00 ?>
We start with December 15, 2021 as our date stored as a DateTime
object inside the variable $date
. Adding an interval of 18 days and 6 minutes to it takes us to January 02, 2022 at 00:06:00.
The add()
method takes care of the days in a specific month and returns the added value accordingly so you don’t have to worry about leap years or different months having 28, 30, or 31 days.
<?php $date = new DateTime('2022-01-29'); echo $date->format('M d, Y'); // Jan 29, 2022 $interval = new DateInterval('P25D'); for($i = 0; $i <= 4; $i++) { $date->add($interval); echo "n".$date->format('M d, Y'); } /* Feb 23, 2022 Mar 20, 2022 Apr 14, 2022 May 09, 2022 Jun 03, 2022 */ ?>
You can see from the following example that the output changes when the initial date is from a leap year.
<?php $date = new DateTime('2024-01-29'); echo $date->format('M d, Y'); // Jan 29, 2024 $interval = new DateInterval('P25D'); for($i = 0; $i <= 4; $i++) { $date->add($interval); echo "n".$date->format('M d, Y'); } /* Feb 23, 2024 Mar 19, 2024 Apr 13, 2024 May 08, 2024 Jun 02, 2024 */ ?>
Specifying Weeks and Days Together in a DateInterval
Combining W with D has only been possible since PHP 8.0.0 when creating a date interval. This means that different versions of PHP will return different dates when running the exact same code. Here is an example:
<?php $date = new DateTime('2024-01-29'); echo $date->format('M d, Y H:i:s')."n"; $interval = new DateInterval('P2W2DT25H'); for($i = 0; $i <= 4; $i++) { $date->add($interval); echo "n".$date->format('M d, Y H:i:s'); } ?>
Two things to note here are the use of W and D together, and that the number of hours in the interval is above 24. This effectively makes the interval (14 + 2 + 1 = ) 17 days and 1 hour. So, PHP should add that with each iteration. The output when running the above code in PHP 8.0.3 is as expected.
Jan 29, 2024 00:00:00 Feb 15, 2024 01:00:00 Mar 03, 2024 02:00:00 Mar 20, 2024 03:00:00 Apr 06, 2024 04:00:00 Apr 23, 2024 05:00:00
However, PHP 7.4 only adds 3 days and 1 hour with each iteration while ignoring the 2 weeks period.
Jan 29, 2024 00:00:00 Feb 01, 2024 01:00:00 Feb 04, 2024 02:00:00 Feb 07, 2024 03:00:00 Feb 10, 2024 04:00:00 Feb 13, 2024 05:00:00
Final Thoughts
I have only mentioned the add()
method used to add a time interval like days and years to a date in PHP. However, PHP also has a sub()
method that you can use to subtract any time interval from a date. Both these methods return the same DateTime
object after modifying its value. You should consider using the DateTimeImmutable
class if you want to prevent the original date from changing.