简体   繁体   中英

Laravel (Carbon) display date difference only in days

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

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

Thanks!

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

$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.

diffInDays function would help.

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

这是最好的尝试这个

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

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

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

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

$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

You just need to use startOfDay() function

for example :

$now = Carbon::now();
$now = $now->startOfDay();

that's it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM