我有以下格式的两个日期:
开始日期= 2009年10月30日
结束日期= 2009年11月30日
如何用PHP计算这两个日期之间的秒数?
答案 0 :(得分:5)
使用strtotime
将两个日期解析为Unix时间戳,然后得到差异:
$firstTime = strtotime("30-10-2009");
$secondTime = strtotime("30-11-2009");
$diff = $secondtime - $firstTime;
答案 1 :(得分:2)
函数strtotime()
会将日期转换为unix样式的时间戳(以秒为单位)。然后,您应该能够从开始日期中减去结束日期以获得差异。
$difference_secs = strtotime($end_date) - strtotime($start_date);
答案 2 :(得分:1)
我建议使用内置的DateTime对象。
$firstTime = new DateTime("30-10-2009");
$diff = $firstTime->diff(new DateTime("30-11-2009"));
至于我,它更灵活,面向OOP。
答案 3 :(得分:1)
实际上,之前的回答会给你一个DateInterval对象,但不是秒。为了通过OOP方法获得秒数,您应该这样做:
$date1 = new DateTime("30-10-2009");
$date2 = new DateTime("30-11-2009");
$seconds = $date2->getTimestamp() - $date1->getTimestamp();