简体   繁体   中英

How to decrease 1 second to a given time

my output right now is increasing instead of decreasing when I even subtract it Here I got 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);

And an index.php which will I display the time

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>

Try getting the difference between the two DateTime values and then formatting it into seconds:

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

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

I should just change

$differenceinseconds = $timefirst - $timesecond; 

to

$differenceinseconds = $timesecond - $timefirst;

I would do it with DateTime and DateInterval.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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