25

Hello all i am trying to add 30 days to my date. I am using below coding.

<?php
$next_due_date = date('05/06/2016', strtotime("+30 days"));
echo $next_due_date;
?>

But it is returning to "05/06/2016" only!

Please help me!

Rahul Mukati
  • 744
  • 2
  • 10
  • 15

5 Answers5

47

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
zanderwar
  • 3,440
  • 3
  • 28
  • 46
21

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);
phreakv6
  • 2,135
  • 1
  • 9
  • 11
  • well i am fetching date from mysql $next_due_date = date('d/m/Y',strtotime('+30 days',strtotime(str_replace('/', '-', '$userRow3["due_date"]')))) . PHP_EOL; And this is returning - 31/01/1970 – Rahul Mukati Jun 06 '16 at 04:31
  • If you are using mysql, then use the first function. mysql dates are of the format yyyy-mm-dd. The first one will work fine for that. Just change the format in the beginning appropriately to either m/d/Y or d/m/Y. – phreakv6 Jun 06 '16 at 04:34
  • i am fetching date from MYSQL and want to add 30 days in it.. check below code if it is right:- – Rahul Mukati Jun 06 '16 at 04:44
  • You dont need an echo there, nor the PHP_EOL. Just use `` – phreakv6 Jun 06 '16 at 04:48
  • 1
    @Rahul so only now you mention mysql, makes a difference, you can do it with a mysql function instead. –  Jun 06 '16 at 04:57
  • Yes doing this in mysql is the way to go. I have updated my answer. @Rahul, you might want to try this approach as well. – phreakv6 Jun 06 '16 at 05:04
17

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

chris85
  • 23,846
  • 7
  • 34
  • 51
  • @Rahul Yes, that is what you previously had. What is `06/05/2016`, that looks like today/yesterdays date. The `strtotime` will start at the current date by default if the second parameter is empty. – chris85 Jun 06 '16 at 04:05
  • @Rahul don't use `"06/05/2016"`, use `"d/m/Y"` like @chris85 said! – Zeke Jun 06 '16 at 04:06
  • but then it will add 30days to todays date!! i want to add 30 days to my own date – Rahul Mukati Jun 06 '16 at 04:09
  • @Rahul so you didn't read my answer, or the supplied link? Specifically from the link `The timestamp which is used as a base for the calculation of relative dates.` Specifically from my answer, `particular starting date use the second parameter of the strtotime function to tell it where to start.`. – chris85 Jun 06 '16 at 04:35
  • @chris - i am fetching date from MYSQL and want to add 30 days in it.. check below code if it is right:- – Rahul Mukati Jun 06 '16 at 04:43
  • Don't do `echo` it is suppose to be the exact date string. Just `$userRow3["due_date"]'` in the `strtotime`. – chris85 Jun 06 '16 at 04:45
10

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;
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
3
$date = "1998-08-14";
$newdate = strtotime ( '30 day' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );

echo $newdate;

Found the above code

Prasad
  • 616
  • 7
  • 11