[Solved] subtract from hour not in time format in php


Use explode() to separate the hour / minute pieces, then do the math yourself:

list( $hour, $min) = explode( ':', "192:40");
list( $hour_sub, $min_sub) = explode( ':', "02:30");
$result = ($hour - $hour_sub) . ':' . ($min - $min_sub);
echo $result;

This will print:

190:10

If the time were to wrap around, ($min - $min_sub) would be negative. To account for this, the following can be used:

list( $hour, $min) = explode( ':', "192:40");
list( $hour_sub, $min_sub) = explode( ':', "02:30");
$hour = $hour - $hour_sub;
$min  = $min  - $min_sub;
if( $min < 0) {
    $min = 60 + $min; // Note $min is negative
    $hour--; // Decrement hour
}
$result = $hour . ':' . $min;

2

solved subtract from hour not in time format in php