Calculate difference between two dates using Carbon and Blade
Solution 1
You are not following the example from the Carbon Documentation. The method Carbon::createFromDate()
expects 4 parameters: year, month, day and timezone. And you are trying to pass a formatted date string.
If you want to create a Carbon object from a formatted date string you can use the constructor of the class just like this:
$date = "2016-09-17 11:00:00";
$datework = new Carbon($date);
Or you can use the static Carbon::parse()
method:
$date = "2016-09-17 11:00:00";
$datework = Carbon::parse($date);
For your purposes you can use the this full example:
$date = Carbon::parse('2016-09-17 11:00:00');
$now = Carbon::now();
$diff = $date->diffInDays($now);
And then in your Blade template:
<td> {{ $diff }} </td>
Solution 2
Blade Template
A shorter code
{{ $diff = Carbon\Carbon::parse($data->last_updated)->diffForHumans() }}
Result : 6 minutes ago
Solution 3
You code can be cleaned up and have the commented out code removed by doing:
<td>{{ $diff = Carbon\Carbon::parse($work['date'])->diffForHumans(Carbon\Carbon::now()) }} </td>
Solution 4
Shortest way
We can directly write it in blade
<span>{{ \Carbon\Carbon::parse( $start_date )->diffInDays( $end_date ) }}</span>
Solution 5
Carbon means you do not need to mix PHP Datetime and Carbon. Once you have the datetime as a Carbon, simply do this...
$comparisonTimeAsCarbon->diffAsCarbonInterval($theOtherTimeAsCarbon)
You can change diffAsCarbonInterval
to diffAsSeconds
, diffAsMinutes
and many more.
diffForHumans
is one of my faves.
Or, choose your own format with...
$comparisonTimeAsCarbon->diff($theOtherTimeAsCarbon)->format('%I:%S')
Carbon will even let you add text instead of a Carbon time, but, I recommend you use Carbon before you parse it, just in case.
Admin
Updated on July 05, 2022Comments
-
Admin almost 2 years
Does anyone know how to pass a given variable instead the Carbon's default parameters ?
The documentation of Carbon says:
// CARBON SAMPLE $dtToronto = Carbon::createFromDate(2012, 1, 1, 'America/Toronto'); $dtVancouver = Carbon::createFromDate(2012, 1, 1, 'America/Vancouver'); echo $dtVancouver->diffInHours($dtToronto); // 3
And i want to do something like this in my controller:
// EXAMPLE $date = "2016-09-16 11:00:00"; $datework = Carbon::createFromDate($date); $now = Carbon::now(); $testdate = $datework->diffInDays($now);
And retrieving that on a Blade template
// VIEW ON BLADE <td> {{ $testdate }} </td>
-
Admin over 7 yearsWell after many tries, this is the only way that works:
<td> <!-- {{ $datework = Carbon\Carbon::parse($work['date']) }} {{ $now = Carbon\Carbon::now() }} // for this --> {{ $diff = $datework->diffForHumans($now) }} </td>
It's all calculate directely the view layer, adding html comments code to remove the Blade output... -
iivannov over 7 years@MarcoFacc it's highly preferable to not do those calculations in the view template.
-
Admin over 7 yearsideally i want to calculate these dates inside his own controller, but the information that i need are retrieved on the view layer via foreach cycle on the $work array. I'm still trying to make something more clean, and put it inside the controller but for now i haven't found any better solution.
-
Crypcode almost 4 yearsIs it possible to use if condition somehow. For example if ($diff > 10 ) print $diff else print 'short time ammount'
-
CodeGuru almost 4 years@wajih, not that i'm aware of but that's a good question, i do sometimes feel the need for something like that too.
-
themrflibble over 3 yearsCarbon has built in Comparison, there's no need to go back to PHP.
-
iivannov over 3 yearsThat's exactly what's shown in the example - one of the Carbon's built in difference methods -
diffInDays()
-
Joel Mellon almost 3 yearsFYI, Carbon's diffInX() methods default to now, so no need to specify if that's when you're comparing to.
-
Abdullah Iftikhar over 2 yearsThis does not include the start date. I want to get the count in days from start to end like date 16-25 I think return should be 10 days. But returning 9 days.