php Laravel (Carbon) 仅以天为单位显示日期差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23336261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:39:19  来源:igfitidea点击:

Laravel (Carbon) display date difference only in days

phpdatelaravel

提问by user2573863

How to display diffrence only by days. Here $price->created_at = 2014-04-28

如何仅按天显示差异。这里$price->created_at = 2014-04-28

\Carbon\Carbon::createFromTimeStamp(strtotime($price->created_at))->diffForHumans()

Thanks!

谢谢!

回答by Jarek Tkaczyk

Suppose you want difference to now() and result from diffForHumans suits you except for today:

假设您想要与 now() 不同并且 diffForHumans 的结果适合您,但今天除外:

$created = new Carbon($price->created_at);
$now = Carbon::now();
$difference = ($created->diff($now)->days < 1)
    ? 'today'
    : $created->diffForHumans($now);

edit: no need to call Carbon::now() twice so use $now instead.

编辑:无需调用 Carbon::now() 两次,因此请改用 $now。

回答by ymutlu

diffInDays function would help.

diffInDays 函数会有所帮助。

$cDate = Carbon::parse($date);
return $cDate->diffInDays();

回答by Hanif Formoly

$DeferenceInDays = Carbon::parse(Carbon::now())->diffInDays($dataToCompare);

this is the best one try this

这是最好的一个试试这个

回答by Subhash Diwakar

When comparing a value in the past to default now: 1 hour ago, 5 months ago

将过去的值与现在的默认值进行比较时:1 小时前、5 个月前

When comparing a value in the future to default now: 1 hour from now, 5 months from now

将未来的值与现在的默认值进行比较时:从现在起 1 小时,从现在起 5 个月

When comparing a value in the past to another value: 1 hour before, 5 months before

将过去的值与另一个值进行比较时:1 小时前、5 个月前

When comparing a value in the future to another value: 1 hour after, 5 months after

将未来的值与另一个值进行比较时:1 小时后,5 个月后

$dt     = Carbon::now();
$past   = $dt->subMonth();
$future = $dt->addMonth();

echo $dt->subDays(10)->diffForHumans();     // 10 days ago
echo $dt->diffForHumans($past);             // 1 month ago
echo $dt->diffForHumans($future);           // 1 month before