Php: Add variable amount days to Y-m-d date format

11,047

However the code is right and works, just in case try

$newDate = date('Y-m-d', strtotime($oldDate. " + {$addedDays} days"));
Share:
11,047

Related videos on Youtube

muffin
Author by

muffin

Noob

Updated on June 04, 2022

Comments

  • muffin
    muffin almost 2 years

    i'm trying to add days to a date with the 'Y-m-d' format:

    $oldDate = '2013-05-15';
    $newDate = date('Y-m-d', strtotime($oldDate. " + 5 days"));
    

    This ouputs '2013-5-20', but below:

    $oldDate = '2013-05-15';
    $addedDays = 5;
    $newDate = date('Y-m-d', strtotime($oldDate. " + $addedDays days"));
    

    doesn't work, it only outputs '1970-01-01', which doesn't make sense because i only tried to put the days to be added in a variable. They're basically the same code. I appreciate the help trying to understand this. Thanks!

  • muffin
    muffin about 10 years
    Ooops, my mistake really. i was adding '-2' days that's why.. anyway thanks for verifying the code