Add one day to a date in PHP
This article demonstrates how to add one day to a date in PHP.
1. Using DateTime::modify() function
The DateTime::modify() function alters the timestamp of a DateTime object by adding or subtracting according to the specified date/time format. To advance a date to the next day, you can use the relative date format "+1 day".
|
1 2 3 4 5 6 7 8 |
<?php $date = new DateTime("2017/12/25 15:30:00"); $date->modify("+1 day"); $result = $date->format("l, d-M-y H:i:s"); var_dump($result); // string(27) "Tuesday, 26-Dec-17 15:30:00" ?> |
If you prefer the procedural style functions, you can use the date_modify() function, which is an alias of DateTime::modify().
|
1 2 3 4 5 6 7 8 |
<?php $date = new DateTime("2017/12/25 15:30:00"); date_modify($date, "+1 day"); $result = date_format($date, "l, d-M-y H:i:s"); var_dump($result); // string(27) "Tuesday, 26-Dec-17 15:30:00" ?> |
You can easily extend the solution to add any given number of days to a date. Consider the following example, which creates an extension method for this simple task:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php function addDays($date, $days, $format="l, d-M-y H:i:s") { $date = new DateTime($date); $date->modify("+$days days"); return $date->format($format); } $date = "2017/12/25 15:30:00"; $days = 10; $result = addDays($date, $days); var_dump($result); // "string(28) "Thursday, 04-Jan-18 15:30:00" ?> |
2. Using DateTime::add() function
You can also modify a DateTime object with the DateTime::add() function, which adds the amount of days, months, years, hours, minutes, and seconds according to the specified DateInterval object. The DateInterval constructor expects a duration parameter for the interval specification. This format starts with P, which stands for period, and is followed by a duration period and a period designator. You can use the period designator D for adding or removing days, as specified by the duration period. Therefore, one day is P1D.
|
1 2 3 4 5 6 7 8 |
<?php $date = new DateTime("2017/12/25 15:30:00"); $date = $date->add(new DateInterval("P1D")); $result = $date->format("l, d-M-y H:i:s"); var_dump($result); // string(27) "Tuesday, 26-Dec-17 15:30:00" ?> |
You may also use the procedural-style function date_add(), which is an alias of the object-oriented style function DateTime::add().
|
1 2 3 4 5 6 7 8 |
<?php $date = date_create("2017/12/25 15:30:00"); $date = date_add($date, new DateInterval("P1D")); $result = date_format($date, "l, d-M-y H:i:s"); var_dump($result); // string(27) "Tuesday, 26-Dec-17 15:30:00" ?> |
You can even write your own utility method for adding any number of days to the given date.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php function addDays($date, $days, $format="l, d-M-y H:i:s") { $date = new DateTime($date); $date = $date->add(new DateInterval("P{$days}D")); return $date->format($format); } $date = "2017/12/25 15:30:00"; $days = 10; $result = addDays($date, $days); var_dump($result); // "string(28) "Thursday, 04-Jan-18 15:30:00" ?> |
3. Using DateTime constructor
You can even use the DateTime constructor to add one day to a given date. The DateTime constructor returns a new DateTime object initialized from the specified date/time string. The idea is to use the English textual datetime description of "+1 day" to advance the given date by one. It can be used as shown below:
|
1 2 3 4 5 6 7 8 |
<?php $str = "2017/12/25 15:30:00"; $dt = new DateTime("$str +1 day"); $result = $dt->format("l, d-M-y H:i:s"); var_dump($result); // string(27) "Tuesday, 26-Dec-17 15:30:00" ?> |
Additionally, if you just need to add one day to the current date, you can directly pass the relative date format "+1 day" to DateTime constructor, as shown below:
|
1 2 3 4 5 6 |
<?php $date = new DateTime("+1 day"); $result = $date->format("l, d-M-y H:i:s"); var_dump($result); ?> |
4. Using strtotime() function
This can be easily done using the strtotime() function, which converts the given date string into a Unix timestamp. You can use the English textual datetime description of "+1 day" to advance the given date by one.
|
1 2 3 4 5 6 7 |
<?php $str = "2017/12/25 15:30:00"; $result = date("l, d-M-y H:i:s", strtotime("$str +1 day")); var_dump($result); // string(27) "Tuesday, 26-Dec-17 15:30:00" ?> |
Here’s a generic version of the above code that creates a utility method to add any number of days to the given date.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php function addDays($date, $days, $format="l, d-M-y H:i:s") { $date = strtotime("$date +$days day"); return date($format, $date); } $date = "2017/12/25 15:30:00"; $days = 10; $result = addDays($date, $days); var_dump($result); // "string(28) "Thursday, 04-Jan-18 15:30:00" ?> |
Finally, if you just want to find tomorrow’s date, you can simply pass "+1 day" to the strtotime() function.
|
1 2 3 4 5 |
<?php $timestamp = strtotime("+1 day"); $result = Date("l, d-M-y H:i:s", $timestamp); var_dump($result); ?> |
That’s all there is to adding one day to a date in PHP.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)