2016-05-22 31 views
0

我使用date_diff得到天兩三天之間如下:date_diff傳遞錯誤的差異?

//DATE 
$current_date = date_create(date("m.d.y")); 
$move_date = date_create($move['moving_day']); 

/* Difference between Moving and Current Date */ 
$difference1 = date_diff($current_date, $move_date); 
$date_difference = $difference1->format('%a'); 

的日期是2016年5月30日,電流爲2016年5月22日,但它讓我看到7,但應賜我8.

的print_r給我

DateTime Object 
(
    [date] => 2016-05-22 05:22:16.000000 
    [timezone_type] => 3 
    [timezone] => UTC 
) 

DateTime Object 
(
    [date] => 2016-05-30 00:00:00.000000 
    [timezone_type] => 3 
    [timezone] => UTC 
) 

我在做什麼錯?

+0

請注意那些'05:22:16'小時/分鐘/秒在您當前的日期.....現在有多少整天有'2016-05 -22 05:22:16'和'2016-05-30 00:00:00'?和幾天的時間? –

+1

如果你指定'$ current_date = date_create('today');',那麼你今天會得到'00:00:00',而不是當前時間 –

+0

的確如此。謝謝! date_create聽起來像正確的功能,但我想我必須使用另一個或只是提取日期。 – Isengo

回答

0

的結果是正確的,如果你改變當前時間到2016年5月30日06:00:00將返回8天

+0

謝謝。我認爲date_create只是創建日期,而不是時間。只有日期有功能嗎? – Isengo

+0

try $ current_date = date(「Y」).'-'。date(「m」)。''。date('d')。' 」 .date( 'G')。 ':'(I ')。 ':'(S ');日期。' 日期'。 –

1

,如果你想不依賴於天的時間,換擋時間00:00:00以這種方式:

$current_date = date_create(date("m.d.y"))->modify('midnight'); 
$move_date = date_create($move['moving_day'])->modify('midnight');