Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string to time over 24 hour in php

There is a synatax errro your variable name is $hour but in last you have used $hours.

$time1  = strtotime("02:40:00");
$time2 = strtotime("34:20:00");
$diff  = $time2 - $time1;

$hour   = floor($diff / (60 * 60));
$minute = $diff - $hour * (60 * 60);
//$final = $hours .":".floor( $minute / 60 );
$final = $hour .":".floor( $minute / 60 );

echo $final;
like image 204
Ifadak Avatar asked Oct 28 '22 21:10

Ifadak


1 Answers

You can do with a custom function like below:-

<?php

$actual_time = "34:20";

$time_to_reduce = "2:40";

function timeSubtactionFirstTime($actual_time ,$time_to_reduce){
   $actual_time_array = explode(":",$actual_time);
   $time_to_reduce = explode(":",$time_to_reduce);
   $final_result = [];
   if($actual_time_array[1] < $time_to_reduce[1]){
     $actual_time_array[0] = $actual_time_array[0]-1;
     $final_result[] = $actual_time_array[1]+60-$time_to_reduce[1];
   }else{
     $final_result[] = $actual_time_array[1]-$time_to_reduce[1];
   }
    $final_result[] = $actual_time_array[0]-$time_to_reduce[0];

    return implode(":", array_reverse($final_result));
}

echo timeSubtactionFirstTime($actual_time ,$time_to_reduce);

Output:-https://eval.in/920129

Note:- i din't consider seconds here. a bit mor logic is reuired for that

like image 162
Anant Kumar Singh Avatar answered Nov 15 '22 06:11

Anant Kumar Singh