且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

计算两个日期之间的差异,小时和分钟

更新时间:2023-01-30 09:11:21

尝试:



功能:

  function date_difference($ date1timestamp,$ date2timestamp){
$ all = round(($ date1timestamp - $ date2timestamp)/ 60);
$ d = floor($ all / 1440);
$ h = floor(($ all - $ d * 1440)/ 60);
$ m = $ all - ($ d * 1440) - ($ h * 60);
//既然你只需要几个小时,最少
返回数组('hours'=> $ h,'mins'=> $ m);
}

调用函数:



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $


I use this code to calculate difference between two dates in hours and minutes :

$all = round(($date1timestamp - $date2timestamp) / 60);
$d = floor ($all / 1440);
$h = floor (($all - $d * 1440) / 60);
$m = $all - ($d * 1440) - ($h * 60);

Works perfectly but when I must multiple times to calculate difference between dates I must write code again and change values is there any simple way to calculate multiple times without rewrite code again ??

Try:

Function:

function date_difference ($date1timestamp, $date2timestamp) {
$all = round(($date1timestamp - $date2timestamp) / 60);
$d = floor ($all / 1440);
$h = floor (($all - $d * 1440) / 60);
$m = $all - ($d * 1440) - ($h * 60);
//Since you need just hours and mins
return array('hours'=>$h, 'mins'=>$m);
}

Calling the function:

$result = date_difference($date1timestamp, $date2timestamp);