Get total minute and second difference between two dates using php

This is a simple guide on how to calculate the difference between two dates and get total minute and second in PHP. Be sure to try out the examples below if you’re new to the topic. You may also like Two date get hour and minute using php.

Code

$t1 = strtotime('2018-03-07 10:10:10');
$t2 = strtotime(date('Y-m-d H:i:s'));
$delta_T = abs($t2 - $t1);
$totalminutes = round(((($delta_T % 604800) % 86400)) / 60); 
$totalsec = round((((($delta_T % 604800) % 86400) % 3600) % 60));

$minLen_minit = strlen($totalminutes);
$second       = strlen($totalsec);

if($minLen_minit >= '2'){
    $minutes = $totalminutes;
}else{
    $minutes = '0'.$totalminutes;
}

if($second >= '2'){
    $second_co = $totalsec;
}else{
    $second_co = '0'.$totalsec;
}
echo  $minutes.':'.$second_co;

Leave a Reply

Your email address will not be published. Required fields are marked *