Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel (Carbon) display date difference only in days

Tags:

date

php

laravel

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

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

Thanks!

like image 318
user2573863 Avatar asked Apr 28 '14 08:04

user2573863


People also ask

How do you find the difference between two dates in laravel Carbon?

You can only use the diffInDays() function on a Carbon instance. You can create a new one by parsing the end date you're receiving. $end = Carbon::parse($request->input('end_date'));

How do I change the date format in Carbon?

Carbon::parse($client->db)->format('d/m/y'); is probably what you're wanting. (parse the yyyy-mm-dd date in the database as a date, and convert it to d/m/y format).


Video Answer


2 Answers

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.

like image 60
Jarek Tkaczyk Avatar answered Sep 18 '22 23:09

Jarek Tkaczyk


diffInDays function would help.

$cDate = Carbon::parse($date); return $cDate->diffInDays(); 
like image 28
ymutlu Avatar answered Sep 22 '22 23:09

ymutlu