Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

php - Calculate the hours minutes and days of difference between two date and hours

I have a PHP and MySQL code that should calculate the hours minutes and days of difference between two date and hours. It works well, just adding 20 hours and 20 minutes more than normal. And I remove the DATE part and put the date and time manually, it works fine.

I don't understand what happens.

    $fecha = $row["fecha"];
        $data= $row["hora"];
$start = strtotime("$fecha $hora"); 


$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentDate =  date("Y-m-d H:i:s", strtotime($currentDate .$currentTime));

$end = strtotime("$currentDate"); 



$totaltime = ($end - $start)  ; 

$hours = intval($totaltime / 3600);   
$seconds_remain = ($totaltime - ($hours * 3600)); 

$minutes = intval($seconds_remain / 60);   
$seconds = ($seconds_remain - ($minutes * 60));
$statusfichaje= $row["status"];
if ($statusfichaje == Start){echo '<td>Trabajando'.$hours.':'.$minutes.':'.$seconds.' </td>';}else{echo '<td>'. $row["status"] .'</td>';}

Edit

start 2019-12-29 21:27:50 . end 2019-12-31 0:51:50 = 47:51:16

As you can see it calculates badly.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A simple example like this would do the job :

$mydatetime = new DateTime();
$datefromdb = new DateTime('2018-03-05 10:10:00');
$interval = $mydatetime->diff($datefromdb);
$date_count = $interval->format('%y years %m months %a days %h hours %i minutes %s seconds');
echo $date_count;

This is your code it should work

$fecha = $row["fecha"];
$data= $row["hora"];
$start = strtotime("$fecha $data"); 


$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentDate =  date("Y-m-d H:i:s", strtotime($currentDate .$currentTime));

$end = strtotime("$currentDate"); 



$totaltime = ($end - $start)  ; 

$hours = intval($totaltime / 3600);   
$seconds_remain = ($totaltime - ($hours * 3600)); 

$minutes = intval($seconds_remain / 60);   
$seconds = ($seconds_remain - ($minutes * 60));
$statusfichaje= $row["status"];
if ($statusfichaje == $start){echo '<td>Trabajando'.$hours.':'.$minutes.':'.$seconds.' </td>';}else{echo '<td>'. $row["status"] .'</td>';}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...