Last Updated on July 28, 2022 by Roshan Parihar
In this tutorial, learn how to add days to date using PHP. The short answer is to use the strtotime()
function of PHP and pass the date with addition of number of days.
You can also add variable days to add the your required date. Both the data and the number of days can be a variable in PHP. You can use these variables to add days to date in PHP.
Let’s see some example that can makes it easier to learn the method.
How to Add Days to Date in PHP
If you want to add days to date in PHP, you can use the strtotime()
function. There are two elements you have to pass to the function. The first element is the number of days and the second is the number of days to add the date. See the below example to learn the method.
1 2 3 4 |
<?php $Date = "2022-07-27"; echo date('Y-m-d', strtotime($Date. ' + 15 days')); ?> |
Output
The above example contains a variable $date
that stores the dates. After that, it uses the strtotime()
function to add 15 days to the specified data. The output shows that 15 days is added to the specified data in PHP.
PHP Use Variables for Adding Days
In addition to the above example, you can also make the number days as a variable to add to the specified dates in PHP. To do this, you have to use the method as given in the example below.
1 2 3 4 5 |
<?php $Date = "2022-07-27"; $AddDays = 15; echo date('Y-m-d', strtotime($myDate. ' + '.$AddDays.' days')); ?> |
Output
If you want to increase the number of day dynamically to the specified dates in PHP, you can use the above example. When you want to find out different dates after adding different number of day, the above methods can be useful for you.
The dynamic addition can be done using the variables in the function in PHP. It can make adding dynamic dates easier in the output.
You May Also Like to Read
- PHP Date function With Examples on Formatting Dates
- How to Convert or Change Date Format to Another in PHP
I hope you like this post on increase the dates by adding the numbers to it.
Reference: Stackoverflow