在 PHP 中计算 2 小时之间的差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16879373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:50:35  来源:igfitidea点击:

Calculate difference between 2 times in hours in PHP

phpdatetimetimedate-arithmetic

提问by Deval Khandelwal

I have two times - For eg- the current time - 08:24 and date is 02/01/2013 in dd/mm/yyyy format and I have another time at 13:46 and date is 31/12/2012 . So, how can I calculate the difference between the 2 times in hours using PHP. (i.e. 42.63 hours) Thanks in advance.

我有两次 - 例如 - 当前时间 - 08:24 和日期是 02/01/2013 的 dd/mm/yyyy 格式,我在 13:46 有另一个时间,日期是 31/12/2012 。那么,如何使用 PHP 计算 2 次之间的差异(以小时为单位)。(即 42.63 小时) 提前致谢。

回答by jcsanyi

Convert them both to timestamp values, and then subtract to get the difference in seconds.

将它们都转换为时间戳值,然后减去以获得以秒为单位的差异。

$ts1 = strtotime(str_replace('/', '-', '02/01/2013 08:24'));
$ts2 = strtotime(str_replace('/', '-', '31/12/2012 13:46'));
$diff = abs($ts1 - $ts2) / 3600;

回答by salathe

Another way is to use PHP's date-related classes. The example below uses DateTime::diff()to get a DateIntervalobject ($interval). It then uses the interval's properties to arrive at the total number of hours in the interval.

另一种方法是使用 PHP 的日期相关类。下面的示例DateTime::diff()用于获取DateInterval对象 ( $interval)。然后它使用间隔的属性来得出间隔中的总小时数。

$a = DateTime::createFromFormat('H:i d/m/Y', '08:24 02/01/2013');
$b = DateTime::createFromFormat('H:i d/m/Y', '13:46 31/12/2012');

$interval = $a->diff($b);
$hours    = ($interval->days * 24) + $interval->h
          + ($interval->i / 60) + ($interval->s / 3600);

var_dump($hours); // float(42.633333333333)

回答by Pooja Yadav

I got a simple solution, Try this one -

我有一个简单的解决方案,试试这个 -

echo getTimeDiff("10:30","11:10");

function getTimeDiff($dtime,$atime)
    {
        $nextDay = $dtime>$atime?1:0;
        $dep = explode(':',$dtime);
        $arr = explode(':',$atime);
        $diff = abs(mktime($dep[0],$dep[1],0,date('n'),date('j'),date('y'))-mktime($arr[0],$arr[1],0,date('n'),date('j')+$nextDay,date('y')));
        $hours = floor($diff/(60*60));
        $mins = floor(($diff-($hours*60*60))/(60));
        $secs = floor(($diff-(($hours*60*60)+($mins*60))));
        if(strlen($hours)<2){$hours="0".$hours;}
        if(strlen($mins)<2){$mins="0".$mins;}
        if(strlen($secs)<2){$secs="0".$secs;}
        return $hours.':'.$mins.':'.$secs;
    }

回答by Niet the Dark Absol

If you have the dates as timestamps (use strtotimeif needed), then just subtract them, optionally take the absolute value, then divide to 3600 (number of seconds in an hour). Easy ^_^

如果您将日期作为时间戳(strtotime如果需要使用),则只需减去它们,可选择取绝对值,然后除以 3600(一小时内的秒数)。简单^_^

回答by Codemaker

I think the following code is useful to get an idea about how to calculate time difference using PHP

我认为以下代码对于了解如何使用 PHP 计算时差很有用

function date_diff($date_1 , $date_2 , $format) {
    $datetime1 = date_create($date_1);
    $datetime2 = date_create($date_2);

    $diff = date_diff($datetime1, $datetime2);

    return $diff->format($format);
}

The above function is useful to calculate difference between two times as well as dates. The dates are given as arguments with the output format.

上面的函数对于计算两个时间和日期之间的差异很有用。日期作为输出格式的参数给出。

The output format are given below:

输出格式如下:

// '%y Year %m Month %d Day %h Hours %i Minute %s Seconds' => 1 Year 3 Month 14 Day 11 Hours 49 Minute 36 Seconds // '%y Year %m Month %d Day' => 1 Year 3 Month 14 Days // '%m Month %d Day' => 3 Month 14 Day // '%d Day %h Hours' => 14 Day 11 Hours // '%d Day' => 14 Days // '%h Hours %i Minute %s Seconds' => 11 Hours 49 Minute 36 Seconds // '%i Minute %s Seconds' => 49 Minute 36 Seconds // '%h Hours => 11 Hours // '%a Days

// '%y 年 %m 月 %d 天 %h 小时 %i 分钟 %s 秒' => 1 年 3 月 14 天 11 小时 49 分 36 秒 // '%y 年 %m 月 %d 天' = > 1 年 3 月 14 天 // '%m 月 %d 天' => 3 月 14 天 // '%d 天 %h 小时' => 14 天 11 小时 // '%d 天' => 14 天// '%h 小时 %i 分 %s 秒' => 11 小时 49 分 36 秒 // '%i 分钟 %s 秒' => 49 分 36 秒 // '%h 小时 => 11 小时 // ' %a 天

回答by Mohd Abdul Mujib

Just putting this here, for anyone who needs to find the difference between two dates/timestamps in Hours, Minutes'AND' Seconds!!

只需将其放在这里,对于需要在Hours, Minutes' AND' 中找到两个日期/时间戳之间差异的任何人Seconds

$futureDate_raw = '12/13/2018'; // This is your finish date.
$fdate = strtotime($futureDate_raw);

$hours   = (($fdate - time()) / 3600;
$mins    = (($fdate - time()) % 3600) / 60;
$seconds = ((($fdate- time()) % 3600) % 60);

回答by Arati

I found this is simplest way to find time difference, it always works for me

我发现这是找到时差的最简单方法,它总是对我有用

$timestamp1 = strtotime(date('Y-m-d H:i'));
$timestamp2 = strtotime("2020-04-05 18:00");

  $diff = abs($timestamp2 - $timestamp1)/(60*60);
echo $diff;