PHP倒数计时器使用' d-m-Y H:i:s'时间戳

时间:2016-01-29 14:37:54

标签: php date timer timestamp countdown

我试图创建一个计时器,从时间戳开始1小时显示倒计时然后显示表单按钮但是我无法使其工作,我尝试了多种方法,我在网上找到但是他们显示错误的时间,或从不显示表单。以下是我到目前为止的情况:

while($row2 = $result2->fetch_assoc()) {
$timestamp = $row2['timestamp'];
$work_id = $row2['work_id'];
$current_time = date("Y-m-d H:i:s"); 
$ready_time = date("Y-m-d H:i:s", strtotime($timestamp)+3600);
if($current_time > $ready_time){?>
<form action="" method="POST" onsubmit="return confirm('Are you sure?');">
    <input type="hidden" value="<?echo $work_id;?>" name="work_id">
    <input type="submit" name="submit" value="Complete Workorder">
</form>
?
}else{
$time1 = new DateTime($current_time);
$time2 = new DateTime($timestamp);
$interval = $time1->diff($time2);

echo $interval->d ." Days ". $interval->h . " Hours, " . $interval->i." Mintues, ".$interval->s." seconds"; 
}
}

1 个答案:

答案 0 :(得分:0)

您可以计算sql中剩余的时间:

SELECT work_id, TIMESTAMPDIFF(SECOND,CURRENT_TIMESTAMP(),`timestamp`) AS timeleft 
FROM tablename

然后在php中检查该值:

$timeleft = $row2['timeleft'];
$work_id = $row2['work_id'];

if($timeleft >=0): ?>
    <form action="" method="POST" onsubmit="return confirm('Are you sure?');">
        <input type="hidden" value="<?echo $work_id;?>" name="work_id">
        <input type="submit" name="submit" value="Complete Workorder">
    </form>
<?php else: ?>
    <p>You need to wait <?php echo $timeleft;?> seconds</p>
<?php endif;

如果您想要在没有刷新的情况下进行实际更新的倒计时,则需要使用javascript代替<p>You need to wait <?php echo $timeleft;?> seconds</p>