Add 30 days to date
Please try this.
echo date('m/d/Y',strtotime('+30 days',strtotime('05/06/2016'))) . PHP_EOL;
This will return 06/06/2016
. Am assuming your initial date was in m/d/Y format. If not, fret not and use this.
echo date('d/m/Y',strtotime('+30 days',strtotime(str_replace('/', '-', '05/06/2016')))) . PHP_EOL;
This will give you the date in d/m/Y format while also assuming your initial date was in d/m/Y format. Returns 05/07/2016
If the input date is going to be in mysql, you can perform this function on mysql directly, like this.
DATE_ADD(due_date, INTERVAL 1 MONTH);
You need to provide date format like d/m/Y
instead of 05/06/2016
Try
$old_date = '05-06-2016';
$next_due_date = date('d-m-Y', strtotime($old_date. ' +30 days'));
echo $next_due_date;
Do not use php's date() function, it's not as accurate as the below solution and furthermore it is unreliable in the future.
Use the DateTime class
<?php
$date = new DateTime('2016-06-06'); // Y-m-d
$date->add(new DateInterval('P30D'));
echo $date->format('Y-m-d') . "\n";
?>
The reason you should avoid anything to do with UNIX timestamps (time()
, date()
, strtotime()
etc) is that they will inevitably break in the year 2038 due to integer limitations.
The maximum value of an integer is 2147483647
which converts to Tuesday, 19 January 2038 03:14:07
so come this time; this minute; this second; everything breaks
Source
Another example of why I stick to using DateTime is that it's actually able to calculate months correctly regardless of what the current date is:
$now = strtotime('31 December 2019');
for ($i = 1; $i <= 6; $i++) {
echo date('d M y', strtotime('-' . $i .' month', $now)) . PHP_EOL;
}
You'd get the following sequence of dates:
31 December
31 November
31 October
31 September
31 August
31 July
31 June
PHP conveniently recognises that three of these dates are illegal and converts them into its best guess, leaving you with:
01 Dec 19
31 Oct 19
01 Oct 19
31 Aug 19
31 Jul 19
01 Jul 19
The first parameter is the format, not the current date.
<?php
$next_due_date = date('d/m/Y', strtotime("+30 days"));
echo $next_due_date;
Demo: https://eval.in/583697
If you want to add the 30 days
to a particular starting date use the second parameter of the strtotime
function to tell it where to start.
When in doubt about how a function works refer to the manual.
http://php.net/manual/en/function.strtotime.php
http://php.net/manual/en/function.date.php