Kitara
Kitara

Reputation: 401

PHP strtotime returning always midnight

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

Upvotes: 12

Views: 1029

Answers (4)

Amit Shah
Amit Shah

Reputation: 1380

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)));

Upvotes: 5

mdamia
mdamia

Reputation: 4557

$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

Upvotes: 3

Ryan Lombardo
Ryan Lombardo

Reputation: 333

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)))

Upvotes: 3

FuzzyTree
FuzzyTree

Reputation: 32392

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)));

Upvotes: 3

Related Questions