user2573863
user2573863

Reputation: 693

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!

Upvotes: 39

Views: 124354

Answers (5)

Ali Safaei
Ali Safaei

Reputation: 347

You just need to use startOfDay() function

for example :

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

that's it!

Upvotes: 0

Subhash Diwakar
Subhash Diwakar

Reputation: 209

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

Upvotes: 1

Hanif Formoly
Hanif Formoly

Reputation: 319

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

this is the best one try this

Upvotes: 10

ymutlu
ymutlu

Reputation: 6715

diffInDays function would help.

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

Upvotes: 39

Jarek Tkaczyk
Jarek Tkaczyk

Reputation: 81187

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.

Upvotes: 81

Related Questions