2009-10-30 59 views

回答

5

使用strtotime解析这两个日期为Unix的时间戳,然后得到的区别:

$firstTime = strtotime("30-10-2009"); 
$secondTime = strtotime("30-11-2009"); 
$diff = $secondtime - $firstTime; 
+0

UNIX时间戳能听懂我扔在日期的任何格式,例如:2009-30-10? – 2009-10-30 09:48:21

+0

这很不错 - 它不应该有'2009-30-10'的问题。你需要小心模糊的日期(“10-09-2009”意味着10月9日或10月9日?)。 – 2009-10-30 10:03:37

+0

是的,为避免这种冲突,最好的格式是什么? - 再次感谢 – 2009-10-30 19:16:52

2

函数strtotime()会将日期转换为unix风格的时间戳(以秒为单位)。然后您应该能够从开始日期减去结束日期以获得差异。

$difference_secs = strtotime($end_date) - strtotime($start_date); 
1

我宁愿建议使用内置的DateTime对象。

$firstTime = new DateTime("30-10-2009"); 
$diff = $firstTime->diff(new DateTime("30-11-2009")); 

至于我这是更灵活,面向对象。

1

事实上,以前的答案会给你一个DateInterval对象,但不是秒。为了得到与面向对象方法,你应该这样做秒:

$date1 = new DateTime("30-10-2009"); 
$date2 = new DateTime("30-11-2009"); 
$seconds = $date2->getTimestamp() - $date1->getTimestamp();