繁体   English   中英

PHP 倒计时 48 小时从设置的日期时间

[英]PHP Countdown 48 hours from a set datetime

我正在寻找在 PHP 中创建一个倒数计时器。 当用户单击一个按钮时,它会将当前日期和时间保存到数据库条目中,然后它应该将该条目与当前日期和时间的差值和 'doSomething' 当差值大于 48 小时。

我的问题是实际的倒计时。

我尝试了以下但无济于事,它只计算两个字符串的差异,而不考虑天数。 不仅如此,它似乎也错误地显示了结果差异:

$d1=strtotime("2012-07-08 11:14:15");
$d2=strtotime("2012-07-09 12:14:15");
$diff = round(abs($d1 - $d2));
$cd = date("H:i:s", $diff);
echo $cd;

感谢您从 StackOverflow 帮助我 Yan.kun! 下面提交的代码是解决方案! 为了严格以小时:分钟:秒为单位显示倒计时,我将 printf() 代码替换为以下内容:

$hours = ($result->d*24)+$result->h;
$minutes = $result->i;
$seconds = $result->s;
echo $hours . ":" . $minutes . ":" . $seconds;

试试这个:

$d1 = new DateTime("2012-07-08 11:14:15");
$d2 = new DateTime("2012-07-09 12:14:15");
$result = $d1->diff($d2);

printf('difference is %d day(s), %d hour(s), %d minute(s)', $result->d, $result->h, $result->i);

编辑:如果您没有可用的 PHP 5.3,您可以将您的时间转换为 unix 纪元时间戳,如本答案中所述

不确定这是否是您想要的:

$d1=strtotime("2012-11-18 11:14:15");//2 days earlier
$d2=strtotime("2012-11-20 11:14:15");//today
$diff = $d2 - $d1; //difference in seconds.

$hours = $diff/60/60;//translation to minutes then to hours

echo $hours;

if($hours>48){
    echo "Script";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM