tags:

views:

306

answers:

3

I'd like to find the date after a date provided by the user

This is not working:

$start_date = '2009-06-10';

$next_day = date( $start_date, strtotime('+1 day') );     

echo $next_day; // 2009-06-10
+2  A: 

Try date_modify:

$d = new DateTime("2009-01-01"); 
date_modify($d, "+1 day"); 
echo $d->format("Y-m-d");

Documentation at http://us3.php.net/manual/en/datetime.modify.php.

kristina
Why not directly use the "modify" method from DateTime?
VVS
DateTime::modify is an alias of date_modify.
kristina
+1  A: 
$start_date = '2009-06-10';

$next_day = date( 'Y-m-d', strtotime( '+1 day', strtotime($start_date) ) );

echo $next_day; // 2009-06-11
meleyal
The second line could be just: $next_day = date('Y-m-d', strtotime($start_date.' +1 day'));
Milen A. Radev
Why does that work!?
meleyal
A: 
$start_date = '2009-06-10';
$next_day = new DateTime($start_date)->modify("+1 day")->format("Y-m-d");

EDIT:

As Kristina pointed out this method doesn't work since DateTime::modify doesn't return the modified date as I suspected. (PHP, I hate your inconsistency!)

This code now works as expected and looks IMHO a little bit more consistent than date_modify :)

$start_date = '2009-06-10';

$next_day = new DateTime($start_date);
$next_day->modify("+1 day")

echo $next_day->format("Y-m-d");
VVS
DateTime::modify() returns NULL, not a DateTime object (the docs are wrong), so this doesn't work.
kristina
Ah, you're right.
VVS