php strotime每天对特定时间倒计时
我一直在尝试创建一个计时器,每天倒计时到下午2点。下午2点后,它应显示到第二天下午2点的小时数。我已经能够在未来的日期使用以下代码,只是不是每天“今天”php strotime每天对特定时间倒计时,php,Php,我一直在尝试创建一个计时器,每天倒计时到下午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));
date_default_timezone_set('America/New_York');
$iTimeTo = strtotime('today 14:00)';
$iDiffTime = $iTimeTo - time();
printf("Remaining: %s\n", date('H:i', $iDiffTime));
输出未显示正确的剩余小时数
$iTimeTo = strtotime('today 14:00)';
应该是:
$iTimeTo = strtotime('today 14:00');
除此之外,我看不出它不起作用的原因,我已经对它进行了测试,它给出了“剩余:20:59”。您可以使用gmdate()
函数将剩余的“秒”格式化为小时:分钟:秒,如下所示:
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
在下午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
?为什么不$time\u to
?剩余时间应为2小时?美国东部时间下午2点(发布此帖子时,目前是美国东部时间12:00),我基于GMT,基于测试环境,我无法强制设置本地时区。@Sébastien-是的,我现在看到了。。谢谢。
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));