如何计算PHP中两个日期之间的天数?
问题描述:
如果我有几个字符串 $startDate
和 $endDate
,它们被设置为(例如)"2011/07/01"
和 "2011/07/17"
(意味着 2011 年 7 月 1 日和 2011 年 7 月 17 日).我如何计算从开始日期到结束日期的天数?在给出的示例中,它将是 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);