我在网上搜索但是看不到多少帮助。
是否有一个现成的函数可以在PHP中找到我尚未找到的函数?
我想计算两个变量日期之间的天数,例如:
$date1 = '01/08/2014'; $date2 = '07/08/2014;
我尝试了以下内容,但$count
为null:
<?php
$date1 = '01/01/14';
$date2 = '07/01/14';
$count = $date1->diff($date2);
echo $count;
?>
答案 0 :(得分:5)
更好:
$from = DateTime::createFromFormat("d/m/Y",$date1);
$to = DateTime::createFromFormat("d/m/Y",$date2);
$diff = $from->diff($to,true);
$days = $diff->days;
答案 1 :(得分:3)
$now = time(); // or your date as well
$your_date = strtotime("2010-01-01");
$datediff = $now - $your_date;
echo floor($datediff/(60*60*24));
答案 2 :(得分:0)
或者您甚至可以使用PHP的date_diff函数:
$interval = $date1->diff($date2);
答案 3 :(得分:0)
使用DateTime :: diff(aka date_diff):
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
Or:
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
然后,您可以通过调用$ interval-&gt; days来获取整数的间隔。