xFredx
xFredx

Reputation: 73

PHP date increase

I've written a piece of code which converts a date to the specific format and increases it by 1 day.

<?php
date_default_timezone_set('Europe/Moscow');
$mehdate = "2011-11-25";
$mehdate = date ('d m Y', strtotime ('+1 day', strtotime($mehdate)));
echo $mehdate, "\n";
?>

But then I have to increase $mehdate by 1 day one more time. And I cannot understand how to do that. I already tried

$mehdate = date ('d m Y', strtotime ("+1 day", $mehdate));

and

$mehdate = date ('d m Y', strtotime ('+1 day', strtotime($mehdate)));

again but it won't work because

strtotime($mehdate)

returns FALSE. So, how can I increase the $mehdate which was already formatted?

Upvotes: 0

Views: 48

Answers (3)

xFredx
xFredx

Reputation: 73

For all newbies like me there is a simple advice: don't use 'd m Y' format, you'd better operate with 'd-m-Y'.

Or you have to use DateTime class, as Object Manipulator advised.

Upvotes: 0

Indrasis Datta
Indrasis Datta

Reputation: 8618

Your issue can easily be resolved if you use DateTime class.

Try this:

$mehdate = new DateTime('2011-11-25');

$mehdate->modify('+1 day');
echo $mehdate->format('d m Y')."\n";  // Gives 26 11 2011

$mehdate->modify('+1 day');
echo $mehdate->format('d m Y');       // Gives 27 11 2011

Upvotes: 3

Syed Ekram Uddin
Syed Ekram Uddin

Reputation: 3091

date_default_timezone_set('Europe/Moscow');
$mehdate = "2011-11-25";
$mehdate = strtotime ('+1 day', strtotime($mehdate));
$mehdate = date ('d m Y', $mehdate);
echo $mehdate, "\n";

Result

26 11 2011

Upvotes: 0

Related Questions