PHP add days on to a specific date?

11,622

Solution 1

Either

$enddate = date(strtotime("+1 day", strtotime($startdate)));

or

$enddate = date(strtotime($startdate . "+1 day"));

should work. However, neither is working with the 18/7/2011 date. They work fine with 7/18/2011: http://codepad.viper-7.com/IDS0gI . Might be some localization problem.

In the first way, using the second parameter to strtotime says to add one day relative to that date. In the second way, strtotime figures everything out. But apparently only if the date is in the USA's date format, or in the other format using dashes: http://codepad.viper-7.com/SKJ49r

Solution 2

try this one, (tested and worked fine)

date('d-m-Y',strtotime($startdate . ' +1 day'));
date('d-m-Y',strtotime($startdate . ' +2 day'));
date('d-m-Y',strtotime($startdate . ' +3 day'));
date('d-m-Y',strtotime($startdate . ' +30 day'));
Share:
11,622
Lee
Author by

Lee

Updated on June 24, 2022

Comments

  • Lee
    Lee almost 2 years

    I am feeling a bit thick today and maybe a little tired..

    I am trying to add days on to a string date...

    $startdate = "18/7/2011";
    
    $enddate = date(strtotime($startdate) . " +1 day");
    echo $startdate;
    echo $enddate;
    

    My heads not with it... where am i going wrong ?

    Thanks

    Lee

  • John Cartwright
    John Cartwright almost 13 years
    This will add two dates together, not add 1 day.
  • NickAldwin
    NickAldwin almost 13 years
    I get the feeling that might work, but I don't have php handy to test. And I can't seem to find that in the docs...
  • NickAldwin
    NickAldwin almost 13 years
    I think you might just be able to do strtotime($startdate . "+1 day") (as in my answer before edit and PtPazuzu's answer) but I am not sure, as I cannot test right now. This should work, though, according to the docs.
  • Lee
    Lee almost 13 years
    this first one gives me 18/7/2011 and 1311961884
  • John Cartwright
    John Cartwright almost 13 years
    I would suggest YOU try it. Look at your first example a little more carefully.
  • NickAldwin
    NickAldwin almost 13 years
    I've tried it out: strtotime seems to be messing up on 18/7 instead of 7/18. With 7/18, both ways work: codepad.viper-7.com/IDS0gI
  • NickAldwin
    NickAldwin almost 13 years
    Here it seems to suggest that for non-American dates, you should use dashes php.net/manual/en/datetime.formats.date.php And dashes seem to work fine: codepad.viper-7.com/SKJ49r