如何检查计时器是否有时间?

时间:2014-05-09 20:17:07

标签: php timer

我创建了一个倒计时,表明人们可以在特定页面上执行特定功能的时间。我已经倒数了,但是他的年龄在0以下,所以他的计数值得进一步下降。

我打印倒计时如下:

-4 days -10 h -3 m -34 s remaining to run 1210 meter

我的倒计时,

$Date = "" . $groups['group_date'] . "";
$stopDate = date('Y-m-d H:i:s', strtotime($Date. ' +' . $periodOfRunning . 'days'));

   <h3><?php    $rem = strtotime($stopDate) - time();
    $day = floor($rem / 86400);
    $hr  = floor(($rem % 86400) / 3600);
    $min = floor(($rem % 3600) / 60);
    $sec = ($rem % 60);
    if($day) echo "$day days ";
    if($hr) echo "$hr h ";
    if($min) echo "$min m ";
    if($sec) echo "$sec s";
    echo " remaining to run " . $totalDistanceToDo . " meter";

            ?></h3>

1 个答案:

答案 0 :(得分:2)

如果你使用DateTime(),你可以比较两次,看看停止时间是否过去。如果是,则显示不同的消息。此外,这使得更容易获得和显示两者之间的间隔:

$now = new DateTime();
$stopdate = new DateTime($stopDate);
if ($stopdate > $now) {
    $diff = $now->diff($stopTime);
    echo $diff->format('%d days, %h hours, %i minutes, %s seconds');
    echo " remaining to run " . $totalDistanceToDo . " meter";
}
else {
    // negative time. display something else
}

请记住,如果您不想为时间单位显示零值,那么它会更复杂一些:

$now = new DateTime();
$stopdate = new DateTime($stopDate);
if ($stopdate > $now) {
    $diff = $now->diff($stopTime);
    if ($diff->d > 0) echo $diff->d . ' days, ';
    if ($diff->h > 0) echo $diff->h . ' hours, ';
    if ($diff->i > 0) echo $diff->i . ' minutes, ';
    if ($diff->s > 0) echo $diff->s . ' seconds';
    echo " remaining to run " . $totalDistanceToDo . " meter";
}
else {
    // negative time. display something else
}