简体   繁体   English

PHP 大于24小时的计算时间差

[英]PHP calc difference between time greater than 24 hours

What is the best way to calculate the difference in time, when time is greater than 24 hours.当时间大于 24 小时时,计算时间差异的最佳方法是什么。

Example例子

$time1 = '76:00:00';
$time2 = '30:00:00';

// result should be 46:00:00
echo date('H:i:s', strtotime($time1) - strtotime($time2));

But this could not be done with this because its greater then 24 hours.但这不能用这个来完成,因为它大于 24 小时。

Also in a database i've stored a time like this: 33:30:00 How in php could i format it to: 33:30同样在数据库中,我存储了这样的时间:33:30:00 如何在 php 中将其格式化为:33:30

Use \DateTime and \DateInterval to perform calculations:使用\DateTime\DateInterval执行计算:

$date1 = new \DateTime('now', new DateTimeZone('UTC'));
$date2 = new \DateTime('now', new DateTimeZone('UTC'));
$time1 = new \DateInterval('PT76H');
$time2 = new \DateInterval('PT30H');

$date1->add($time1);
$date2->add($time2);

$diff = $date1->diff($date2);
echo ($diff->days * 24 + $diff->h) . $diff->format(':%I:%S');

Explanation: It's not possible to perform calculations directly on DateInterval s, so you have to create dates as a basis for calculations.说明:无法直接对DateInterval执行计算,因此您必须创建日期作为计算的基础。 Then add two different intervals to current dates, and calculate a difference between them.然后将两个不同的间隔添加到当前日期,并计算它们之间的差异。 diff() returns \DateInterval that contains total number of days, that you have to multiply by 24 to get hours, and hours that don't make full days. diff()返回包含总天数的\DateInterval ,您必须乘以 24 才能得到小时数,以及不构成全天的小时数。

EDIT: The timezone should be specified as UTC to avoid daylight saving time issues.编辑:时区应指定为 UTC 以避免夏令时问题。

You can try this:你可以试试这个:

/**
 * Common method to diff between two time
 * 
 * @param string $start
 * @param string $end
 * @return string
 */
public function diffTime($start, $end)
{
    /* $start = '76:00:00'; */
    /* $end = '30:00:00'; */
    /* Get total time in Seconds */
    $totalStartTimeSec = self::getTotalSeconds($start);
    $totalEndTimeSec = self::getTotalSeconds($end);
    /* Get difference time in Seconds */
    $difference = abs($totalStartTimeSec - $totalEndTimeSec);
    $hours = floor($difference / 3600);
    $minutes = floor(($difference % 3600) / 60);
    $seconds = $difference % 60;
    $diffFormat = str_pad($hours, 2, '0', STR_PAD_LEFT). ":" . str_pad($minutes, 2, '0', STR_PAD_LEFT). ":" . str_pad($seconds, 2, '0', STR_PAD_LEFT);
    
    /* result $diffFormat is '46:00:00'; */
    return $diffFormat;
}

/**
 * Get total time in Seconds
 * 
 * @param string $time
 * @return array
 */
public function getTotalSeconds($time) {
    $timeArr = explode(':', $time);
    $allTime = [];
    if (isset($timeArr[0]) && $timeArr[0] > 24) {
        $greaterDuration = $timeArr[0];
        while ($greaterDuration > 24) {
            $greaterDuration -= 24;
            $allTime[] = strtotime('24:00:00') - strtotime('00:00');
        }
        if ($greaterDuration == 24 && ($timeArr[1] != '00' || $timeArr[2] != '00')) {
            $greaterDuration -= 24;
            $allTime[] = strtotime('24:00:00') - strtotime('00:00');
        }
        $remainingTime = str_pad($greaterDuration, 2, '0', STR_PAD_LEFT) . ':' . $timeArr[1] . ':' . $timeArr[2];
        $allTime[] = strtotime($remainingTime) - strtotime('00:00');
    } else {
        $allTime[] = strtotime($time) - strtotime('00:00');
    }
    
    return array_sum($allTime);
}

Thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM