PHP 时间每天午夜一小时


PHP Time going mental for one hour at midnight every day

我运行这个脚本,它可以很好地估计与天气相关的事情发生之前需要多长时间。但是午夜它会发疯,在整个午夜时间,它会返回疯狂的负时间,例如 -1100 分钟之类的,然后当它到达 0100 小时时,它会恢复正常并报告,例如 20 分钟等。

脚本:

  $timenow = date("H:i:s");
  $eventtime= strtotime("$gettimefromtextfile"); //time the weather event will happen in the near future
 $TimeEnd = strtotime($timenow);
 $Difference = ($eventtime - $TimeEnd);
if ($Difference >= 0) {
 $minutes = floor(($Difference / 60)); 
// print how many minutes until an event happens, discard it if event is in the past

我知道日期函数在午夜到 PHP 5.3 时有问题。但是我运行的是PHP 5.3,所以应该不是问题。我不需要日期,这只是我需要的时间,与天气相关的东西最多只报告几个小时的差异。

关于替代功能或编码的任何建议,可以在午夜停止这种痉挛?

使用DateTime::diff怎么样?不要重新发明轮子!

<?php
date_default_timezone_set('Europe/Lisbon'); 
$next = new DateTime('18:00:01');
$now = new DateTime();
$diff = $next->diff($now);
echo $diff->format('%h hours, %i minutes');
?>

参考: http://php.net/manual/en/datetime.diff.php