我有两个-例如,当前时间为08:24,日期为02/01/2013,格式为dd/mm/yyyy还有一个时间是13:46,日期是2012年12月31日。那么,我如何使用PHP计算两个小时之间的差异呢?(即42.63小时)
将它们都转换为时间戳值,然后相减以获得秒差。
$ts1 = strtotime(str_replace('/', '-', '02/01/2013 08:24'));
$ts2 = strtotime(str_replace('/', '-', '31/12/2012 13:46'));
$diff = abs($ts1 - $ts2) / 3600;
另一种方法是使用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)
我有一个简单的解决方案,试试这个-
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;
}
如果您有日期作为时间戳(如果需要使用strtotime
),那么只需减去它们,可以选择取绝对值,然后除以3600(一小时内的秒数)。容易^ _ ^
我认为下面的代码对了解如何使用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);
}
上面的函数对于计算两个时间和日期之间的差是有用的。日期作为输出格式的参数给出。
输出格式如下:
//'%y年%m月%d日%h小时%i分钟%s秒' => 1年3月14天11小时49分钟36秒//'%y年%m月%d日' => 1年3月14天//'%m Month %d Day' => 3 Month 14 Day//'%d Day %h Hours' => 14 Day 11 Hours//'%d Day' => 14天//'%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
就放在这里,对于任何需要找到Hours
, Minutes
'和' 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);
我发现这是查找时差的最简单的方法,它对我来说总是有效的
$timestamp1 = strtotime(date('Y-m-d H:i'));
$timestamp2 = strtotime("2020-04-05 18:00");
$diff = abs($timestamp2 - $timestamp1)/(60*60);
echo $diff;
下面的代码可用于获取格式为H:I:S:
的时差方法1:function time_diff($startDateTime, $endDateTime) {
$startDateTime = strtotime(str_replace('/', '-', $startDateTime));
$endDateTime = strtotime(str_replace('/', '-', $endDateTime));
$difference = abs($startDateTime - $endDateTime);
$hours = floor($difference / 3600);
$minutes = floor(($difference % 3600) / 60);
$seconds = $difference % 60;
return str_pad($hours, 2, '0', STR_PAD_LEFT). ":" . str_pad($minutes, 2, '0', STR_PAD_LEFT). ":" . str_pad($seconds, 2, '0', STR_PAD_LEFT);
}
方法二:
function time_diff($startDateTime, $endDateTime) {
$datetime1 = new DateTime($startDateTime);
$datetime2 = new DateTime($endDateTime);
$interval = $datetime1->diff($datetime2);
return $interval->format('%H:%I:%S');
}
谢谢!