倒数计时器变为负数

Countdown timer goes into negative numbers

感谢您的帮助!

抱歉,我的编码经验很基础,我正在慢慢自学。我很困惑为什么这个倒计时在达到 0 时进入负数然后开始计数。

又名,目前是 -3 分 -41 秒。

我希望它在达到 0 时说 "NONE"。

<?
function protectup ($online){
global $time;

$difference=$online-$time;
$num = $difference/86400;
$days = intval($num);
$num2 = ($num - $days)*24;
$hours = intval($num2);
$num3 = ($num2 - $hours)*60;
$mins = intval($num3);
$num4 = ($num3 - $mins)*60;
$secs = intval($num4);

if($days != 0){echo"$days days, ";}
if($hours != 0){echo"$hours hours, ";}
if($mins != 0){echo"$mins minutes and ";}
if($secs != 0) {echo"$secs seconds"; }else{echo"None";}
} ?>

<?=protectup($protect)?>

感谢您的帮助!

你可以有一个没有第二个的剩余时间 您应该为每个部分添加逗号和 a 以及 last

<? 

function protectup ($online) { 
    global $time; 

    $difference=$online-$time; 
    if ($difference <= 0) { 
        echo"None\n"; 
        return; 
    } 
    $num = $difference/86400; 
    $days = intval($num); 
    $num2 = ($num - $days)*24; 
    $hours = intval($num2); 
    $num3 = ($num2 - $hours)*60; 
    $mins = intval($num3); 
    $num4 = ($num3 - $mins)*60; 
    $secs = intval($num4); 

    $out = array(); 
    if($days != 0){$out[] ="$days days";} 
    if($hours != 0){$out[] = "$hours hours";} 
    if($mins != 0){$out[] = "$mins minutes";} 
    if($secs != 0){$out[] = "$secs seconds";} 

    echo $out[0]; 
    for ($i = 1; $i < count($out) - 1; $i++) { 
        echo(", ".$out[$i]); 
    } 
    if (count($out) > 1) { 
        echo(" and ".$out[count($out) - 1]); 
    } 
    echo(".\n"); 
}   

protectup(123); 
protectup(120); 
protectup(124); 
protectup(184); 
protectup(424); 
protectup(345184); 
protectup(0); 
protectup(24 * 3600 + 5); 
protectup(24 * 3600 + 5 * 60); 
?> 

输出

2 minutes.
2 minutes and 3 seconds.
3 minutes and 4 seconds.
7 minutes and 3 seconds.
3 days, 23 hours, 53 minutes and 3 seconds.
None
1 days and 5 seconds.
1 days and 5 minutes.