简体   繁体   中英

Substract an amount of time to an addition of times

I have to substract 32:30:00 (string) to 95:05:00 (string) in php :

95:05:00 - 32:30:00

THey are coming from an addition of time .

I can't find any code working, cause strtotime doesnt accept more than 24 as a value .

Please help me, thank you.

For example, i ve tried this :

$time1 = strtotime('32:30:00');
$time2 = strtotime('95:05:00');
$difference = round(abs($time2 - $time1) / 3600,2);
echo 'différence : '.$difference;

It returns 0

It should return something like 62:35:00

Do you know if i can do it with moment.js or a php lib ?

strtotime does not handle durations, only valid timestamps. You can handle it yourself by breaking apart the times by exploding the timestamp into hours, minutes and seconds. You can then convert them into total seconds.

<?php
$time1 = '95:05:00';
$time2 = '32:30:00';

function timeToSecs($time) {
    list($h, $m, $s) = explode(':', $time);
    $sec = (int) $s;
    $sec += $h * 3600;
    $sec += $m * 60;
    return $sec;
}

$t1 = timeToSecs($time1);
$t2 = timeToSecs($time2);
$tdiff = $t1 - $t2;

echo "Difference: $tdiff seconds";

We can then convert it back into hours minutes and seconds:

$start = new \DateTime("@0");
$end   = new \DateTime("@$tdiff");

$interval = $end->diff($start);

$time = sprintf(
    '%d:%02d:%02d',
    ($interval->d * 24) + $interval->h,
    $interval->i,
    $interval->s
);

echo $time; // 62:35:00

One way to get the difference in seconds is with mktime .

$diff = mktime(...explode(':', $time1)) - mktime(...explode(':', $time2));

There are various ways to convert back to the string format you want. I was going to suggest using sprintf , but the other answer already shows it so I won't bother.

In general, when you're dealing with time intervals, I think it's easier to do all your calculations in seconds and then format your result when you need to output it, so you can avoid needing to do this kind of thing.

Here is an alternative.

$time1 = '32:30:00';
$time2 = '95:05:00';

function timeDiff($time1, $time2)
{
    $time1 = explode(':', $time1);
    $time2 = explode(':', $time2);

    // Make sure $time2 is always the bigger time
    if ($time1[0] > $time2[0]) {
        $temp = $time1;
        $time1 = $time2;
        $time2 = $temp;
    }

    // Work out the difference in each of the hours, minutes and seconds
    $h = abs($time2[0] - $time1[0]);
    $m = abs($time2[1] - $time1[1]);
    $s = abs($time2[2] - $time1[2]);

    // Ensure that it doesn't say "60", since that is not convention.
    $m = $m == 60 ? 0 : $m;
    $s = $s == 60 ? 0 : $s;

    // If minutes 'overflows', then we need to remedy that
    if ($time2[1] < $time1[1]) {
        $h -= $h == 0 ? $h : 1;
        $m = 60 - $m;
    }

    // Fix for seconds
    if ($time2[2] < $time1[2]) {
        $m -= $m == 0 ? -59 : 1;
        $s = 60 - $s;
    }

    // Zero pad the string to two places.
    $h = substr('00'.$h, -2);
    $m = substr('00'.$m, -2);
    $s = substr('00'.$s, -2);

    return "{$h}:{$m}:{$s}";
}

echo timeDiff($time1, $time2);

I simply work out the difference between each hours, minutes, and seconds separately.

I have commented the code accordingly to provide more insight into what is happening at each stage.

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