0

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?

xFredx
  • 73
  • 4
  • Refer to http://stackoverflow.com/questions/660501/simplest-way-to-increment-a-date-in-php – Syed Ekram Uddin Apr 24 '16 at 10:04
  • I found that if I change 'd m Y' to 'd-m-Y' the code will work fine. But I have to display date as 'd m Y' formatted. And actually I want to find a way to convert $mehdate to 'd-m-Y' after the first block of code. – xFredx Apr 24 '16 at 10:14

3 Answers3

3

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
Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32
0
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
Syed Ekram Uddin
  • 2,907
  • 2
  • 29
  • 33
  • Thank you for reply! But what should I do to increase $mehdate one more time? – xFredx Apr 24 '16 at 10:11
  • If the answer solve you problem, choose it as correct answer, so that others can get benefit. there are several ways to solve your questions. however i just help to find problem in your code so that you can continue with what you doing. hope helps – Syed Ekram Uddin Apr 24 '16 at 10:16
  • one more time? then follow @Object Manipulator approach. thats better – Syed Ekram Uddin Apr 24 '16 at 10:20
0

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.

xFredx
  • 73
  • 4