如何减少1秒到给定的时间

时间:2019-03-13 09:55:00

标签: javascript php

我现在的输出在增加甚至减少时都在增加而不是减少 在这里,我得到了responsetime.php

   <?php
session_start();
require_once 'includes/dbh.php';
$sql='SELECT * FROM acceptedorder;';
$stmt=mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt,$sql)){
echo "Eror in statements";
}else{
mysqli_stmt_execute($stmt);
$result=mysqli_stmt_get_result($stmt);
while($row=mysqli_fetch_assoc($result)){
    if($row['fname']!=""){
$duration=$row['duration'];
echo $row['duration'];
echo "<br>";
}}
}

$durated=$row['duration'];
$startime=date('Y-m-d H:i:s');
$end_time=date('Y-m-d 
H:i:s',strtotime('+'.$durated.'minutes',strtotime($startime)));
$_SESSION['endtime']=$end_time;
$from_time=date('Y-m-d H:i:s');
$to_time=$_SESSION['endtime'];
$timefirst=strtotime($from_time);
$timesecond=strtotime($to_time);
$differenceinseconds=$timefirst-$timesecond;
echo gmdate('H:i:s', $differenceinseconds);

还有一个index.php,我将显示时间

session_start();  

<script type="text/javascript">
setInterval(function(){
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET",'responseTime.php',false);
    xmlhttp.send(null);
    document.getElementById('response').innerHTML=xmlhttp.responseText;
},1000);
    </script>

3 个答案:

答案 0 :(得分:1)

尝试获取两个DateTime值之间的差异,然后将其格式化为秒:

$differenceInTime = $from_time->diff($to_time);

$differenceInSeconds $differenceInTime->format("%s");

答案 1 :(得分:1)

我应该改变

$differenceinseconds = $timefirst - $timesecond; 

$differenceinseconds = $timesecond - $timefirst;

答案 2 :(得分:1)

我会用DateTime和DateInterval做到这一点。

$now = new DateTime('now');
$minusOneSecond = $now->sub(new DateInterval('PT1S'));
echo $minusOneSecond->format('d-m-Y H:i:s');