使用diffForHumans()方法使用“ ago”对now()与日期时间的碳差异

arv*_*vil 4 php datetime php-carbon

根据手册:http : //carbon.nesbot.com/docs/#api-humandiff

得到一个 ago

将过去的值与现在的默认值进行比较时

但是无论我做什么,我都无法 ago

return $datetime->diffForHumans(Carbon::now())

结果到before,而

return Carbon::now()->diffForHumans($datetime);

结果after

但是正如您可以清楚地看到,我上面的两个代码段都比较了past($ datetime)和现在的默认值(Carbon :: now()),所以我不明白为什么我找不到以前?希望有人能帮忙。我只需要回声ago。谢谢!

chr*_*con 5

您应diffForHumans()在参数“日期计算”之后使用无参数,例如:

Carbon::now()->subDays(24)->diffForHumans();  // "3 weeks ago"
Run Code Online (Sandbox Code Playgroud)

或者,如果您有日期,则可以使用use $datetime->diffForHumans();

$datetime = Carbon::createFromDate(2015, 8, 25); // or your $datetime of course
return $datetime->diffForHumans();  // "1 week ago"
Run Code Online (Sandbox Code Playgroud)