2017-01-11 28 views
0

我正在嘗試使用Carbon PHP,事情是我是新手,並且我不知道如何繼續。我想獲得一個月內的天數,因此我創建了一個Carbon實例來獲取該月的第一天。我添加了一個月,並嘗試與diffInDays得到的差異,但它返回我0. 我嘗試了很多東西,但我無法解決這個問題。你們有想法嗎?Carbon中的DiffInDays php

這是代碼。

$month = intval($_GET['month'], 10); 
$year = intval($_GET['year'], 10); 


$monthToEvaluate = Carbon::create($year, $month, 1, 0, 0); //It returns a Carbon Instance with the correct date 
$monthAfter = $monthToEvaluate->addMonth(); //It returns a Carbon Instance with the correct date too 

echo $monthAfter->diffInDays($monthToEvaluate, false); //var_dump of this returns int(0) 
+0

'$ monthToEvaluate-> addMonth();''修改$ monthToEvaluate' ....它不是一成不變的,所以'$ monthToEvaluate '和'$ monthAfter'將是相同的 –

回答

0

嘗試這種方式,多見於http://carbon.nesbot.com/docs/

$monthToEvaluate = Carbon::create($year, $month, 1, 0, 0); 
echo $monthToEvaluate->diffInDays($monthToEvaluate->copy()->addMonth());