PHP strtotime 每日倒计时到特定时间


php strtotime daily countdown to a specific time

我一直在尝试创建一个计时器,每天倒计时到下午 2 点。 下午 2 点之后,它应该显示直到第二天下午 2 点的小时数。我已经能够将以下代码用于将来的日期,而不是每天"今天"。

date_default_timezone_set('America/New_York');
$iTimeTo = strtotime('today 14:00)';  
$iDiffTime = $iTimeTo - time();  
printf("Remaining: %s'n", date('H:i', $iDiffTime));  

输出未显示正确的剩余小时数。

strtotime('today 14:00');
下午 2 点

后成为过去,所以你会在下午 2 点到午夜之间开始得到荒谬的结果。

date_default_timezone_set('America/New_York');
$today = strtotime('today 14:00');
$tomorrow = strtotime('tomorrow 14:00');
$now = time();
$timeLeft = ($now > $today ? $tomorrow : $today) - $now;
printf("Remaining: %s'n", gmdate("H:i:s", $timeLeft));
$iTimeTo = strtotime('today 14:00)'; 

应该是:

$iTimeTo = strtotime('today 14:00'); 

除此之外,我认为没有理由它不起作用,我已经测试过它,它给了我"剩余:20:59"。

您可以使用

gmdate()函数将剩余的"秒"格式化为小时:分钟S:秒,如下所示:

date_default_timezone_set('America/New_York');
$iTimeTo = strtotime('today 14:00');  
$iDiffTime = $iTimeTo - time();  
printf("Remaining: %s'n", gmdate("H:i:s", $iDiffTime));

输出:

Remaining: 01:59:55