【发布时间】:2013-10-29 11:45:03
【问题描述】:
我运行这个脚本,它可以很好地估计与天气相关的事情发生之前的时间。但是午夜它变得疯狂,在整个午夜时分,它会返回疯狂的负时间,例如 -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()吗? -
没有,我看了一下,但我不太明白这个功能。我怎么能用它来显示现在的时间而不显示日期?
-
$gettimefromtextfile"看起来像什么?你能给出一些示例值吗? -
它只是从格式为 0215 的文本文件中提取时间,并将其格式化为例如 02:15:00。可能是 strtotime 有问题吗?
-
PHP Time going mental for one hour at midnight every day,老实说,这个问题让我哭笑不得,声音太大了,我想它会在午夜被提出一个小时。为这个有趣的标题 +1,哈哈...