0

我需要这样的东西

$today = date("m-d-Y");
$otherdate = "05-03-2013";

//Some math here

//echo the difference between the two dates in days

我试过使用 strtotime 但它不能正常工作并且通常说差异大约是 15k 天(这显然是不正确的)

有什么帮助吗?

4

3 回答 3

1

尝试diff()方法DateTime

$now  = new DateTime();
$then = DateTime::createFromFormat('m-d-Y', '05-03-2013');

$interval = $now->diff($then);

$days = $interval->format('%a days');
于 2013-05-07T09:46:22.620 回答
0

PHP 5.3+

$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; 
}
于 2013-05-07T09:46:16.810 回答
0

试试这个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";
于 2013-05-07T09:44:39.937 回答