12

I'm having a weird problem. When I do strtotime it's not considering the hours part of the original date, and it's always returning midnight. I tried to research but I couldn't find anything specific.

Is there something I'm missing?

$original_date = "2015-08-07 02:00:00";
$next_date = date('Y-m-d H:i:s', strtotime("monday this Week +1 week", strtotime($original_date)));

It returns $next_date as 2015-08-14 00:00:00

jon snow
  • 3,062
  • 1
  • 19
  • 31
Kitara
  • 401
  • 1
  • 3
  • 15
  • 1
    I'm not sure how to correct it, but giving `strtotime` a specific day like Monday sets it to Monday 00:00:00 and then the other calculations use that. – AbraCadaver Aug 21 '15 at 04:44

4 Answers4

5

Try this, add time which you want to retrieve in next date,.

$original_date = "2015-08-07 02:00:00";
echo $next_date = date('Y-m-d H:i:s', strtotime("monday this Week 02:00:00 +1 week", strtotime($original_date)));
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Amit Shah
  • 1,380
  • 1
  • 10
  • 19
  • actually when saying monday this week, it will take date only and add 00:00:00 mid night internally, so we have just added time in the strtotime. – Amit Shah Aug 21 '15 at 04:47
  • This works! Would you advice to always add the hour as a security blanket? – Kitara Aug 21 '15 at 04:50
  • @Kitara yes you should always add it when you want to retain your hours, mins and secs. – Amit Shah Aug 21 '15 at 04:52
3

monday this week +1 week assumes you’re looking for midnight of the monday of the week of the passed in time. If you want to preserve the hours part of the time, then you can append it to your date format because it should always be the same as in $original_date

date('Y-m-d ' . date('H:i:s', strtotime($original_date)), strtotime("monday this Week +1 week", strtotime($original_date)));
FuzzyTree
  • 32,014
  • 3
  • 54
  • 85
  • Hi, it didn't work for me output was 2015-08-10 19:33:35. I modify to date('Y-m-d', strtotime("monday this Week +1 week", strtotime($original_date))).' '.date('H:i:s', strtotime($original_date)) and it worked. – Kitara Aug 21 '15 at 04:49
  • @Kitara that works too, I was missing the `strtotime` call – FuzzyTree Aug 21 '15 at 04:51
3

When you use monday in strtotime you're resetting the time back to 00:00:00. You will have to explicitly pass the time in either your date or strtotime to get your desired behavior. See this same question for a similar issue.

$next_date = date('Y-m-d H:i:s', strtotime("monday this Week +1 week " . date('H:i:s', strtotime($original_date)), strtotime($original_date)))
Community
  • 1
  • 1
Ryan Lombardo
  • 323
  • 1
  • 10
3
$date = strtotime('2018-08-14 02:00:00');
$next_date = date('Y-m-d H:i:s', strtotime("monday this Week 02:00:00 +1 week", $date)); // 2018-08-20 02:00:00
mdamia
  • 4,447
  • 1
  • 24
  • 23