我需要这样的东西
$today = date("m-d-Y");
$otherdate = "05-03-2013";
//Some math here
//echo the difference between the two dates in days
我试过使用 strtotime 但它不能正常工作并且通常说差异大约是 15k 天(这显然是不正确的)
有什么帮助吗?
尝试diff()
方法DateTime
:
$now = new DateTime();
$then = DateTime::createFromFormat('m-d-Y', '05-03-2013');
$interval = $now->diff($then);
$days = $interval->format('%a days');
$b_date1 = new DateTime($date);
$today1 = new DateTime($today);
$interval = date_diff($today1,$b_date1);
PHP版本低于5.2
function date_diff1($date1, $date2) {
$current = $date1;
$datetime2 = date_create($date2);
$count = 0;
while(date_create($current) < $datetime2){
$current = gmdate("Y-m-d", strtotime("+1 day", strtotime($current)));
$count++;
}
return $count;
}
试试这个Y-m-d
格式化日期
$now = strtotime(date("Y-m-d")); // or your date as well
$otherdate = strtotime("2013-05-03");
$datediff = $now - $otherdate;
echo floor($datediff/(60*60*24)). "Days";