两个日期之间的小时数差异(整数)

时间:2012-11-13 18:15:03

标签: php datetime

  

可能重复:
  Calculate number of hours between 2 dates in PHP

如何在PHP中获取两个日期之间的差异小时数?我需要得到一个整数,因为我想知道它是大于还是小于特定值。

3 个答案:

答案 0 :(得分:4)

尝试:

 $date1 = "2012-11-05 12:35:00";
 $date2 = "2012-11-07 14:35:00"; 
 $diff = strtotime($date2) - strtotime($date1);
 $diff_in_hrs = $diff/3600;
 print_r($diff_in_hrs);

Manual

Demo

答案 1 :(得分:2)

如果你有最新的PHP

$dateOne = new DateTime('2012-01-20 00:00:00');
$dateTwo = new DateTime('2012-01-21 02:00:00');

//程序上     $ interval = date_diff($ dateOne,$ dateTwo);

// Alternatively OOP style if supported
$interval = $dateOne->diff($dateTwo);

请参阅:http://www.php.net/manual/en/class.dateinterval.php

答案 2 :(得分:1)

<?php
$time1 = time();
$time2 = mktime(0,0,0,11,13,2012); // earlier today
echo ($time1 - $time2) / 3600; // 3600 seconds in hour
?>