January 2022


* { box-sizing: border-box; } body {margin: 0;}*{box-sizing:border-box;}body{margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;}

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.

  1. 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.
  2. 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:

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.

You can see from the following example that the output changes when the initial date is from a leap year.

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:

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.

However, PHP 7.4 only adds 3 days and 1 hour with each iteration while ignoring the 2 weeks period.

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.

Comments 0
There are currently no comments.