如何计算PHP中的两个日期之间的天数? [英] How to count days between two dates in PHP?
本文介绍了如何计算PHP中的两个日期之间的天数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有一对字符串$ startDate和$ endDate设置为(例如)2011/07/01和2011/07/17(意味着2011年7月1日和2011年7月17日)。我如何计算从开始日期到结束日期的天数?
If I have a couple of strings $startDate and $endDate which are set to (for instance) "2011/07/01" and "2011/07/17" (meaning 1 July 2011 and 17 July 2011). How would I count the days from start date to end date? In the example given, it would be 17 days.
推荐答案
这是原始的方式。
$startTimeStamp = strtotime("2011/07/01");
$endTimeStamp = strtotime("2011/07/17");
$timeDiff = abs($endTimeStamp - $startTimeStamp);
$numberDays = $timeDiff/86400; // 86400 seconds in one day
// and you might want to convert to integer
$numberDays = intval($numberDays);
这篇关于如何计算PHP中的两个日期之间的天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文