PHP strtotime 总是在午夜返回
PHP strtotime returning always midnight
我遇到了一个奇怪的问题。当我执行 strtotime 时,它不会考虑原始日期的小时部分,而且它总是在午夜返回。我试图进行研究,但找不到任何具体内容。
有什么我遗漏的吗?
$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)));
它returns$next_date
作为2015-08-14 00:00:00
monday this week +1 week
假设您要查找过去一周中星期一的午夜。如果您想保留部分时间的小时数,那么您可以将其附加到您的 date
格式,因为它应该始终与 $original_date
中的相同
date('Y-m-d ' . date('H:i:s', strtotime($original_date)), strtotime("monday this Week +1 week", strtotime($original_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)));
当您在 strtotime
中使用 monday
时,您会将时间重置回 00:00:00。您必须在 date
或 strtotime
中明确传递时间才能获得所需的行为。有关类似问题,请参阅 this same question。
$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)))
$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
我遇到了一个奇怪的问题。当我执行 strtotime 时,它不会考虑原始日期的小时部分,而且它总是在午夜返回。我试图进行研究,但找不到任何具体内容。
有什么我遗漏的吗?
$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)));
它returns$next_date
作为2015-08-14 00:00:00
monday this week +1 week
假设您要查找过去一周中星期一的午夜。如果您想保留部分时间的小时数,那么您可以将其附加到您的 date
格式,因为它应该始终与 $original_date
date('Y-m-d ' . date('H:i:s', strtotime($original_date)), strtotime("monday this Week +1 week", strtotime($original_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)));
当您在 strtotime
中使用 monday
时,您会将时间重置回 00:00:00。您必须在 date
或 strtotime
中明确传递时间才能获得所需的行为。有关类似问题,请参阅 this same question。
$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)))
$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