Calculate difference between two dates using Carbon and Blade

PhpLaravelLaravel BladePhp Carbon

Php Problem Overview


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>

Php Solutions


Solution 1 - Php

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 - Php

Blade Template

A shorter code

{{ $diff = Carbon\Carbon::parse($data->last_updated)->diffForHumans() }}

Result : 6 minutes ago

Solution 3 - Php

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 - Php

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.

Solution 5 - Php

Shortest way

We can directly write it in blade

<span>{{ \Carbon\Carbon::parse( $start_date )->diffInDays( $end_date ) }}</span>

Solution 6 - Php

You can use on a Blade template directly:

{{ (Carbon\Carbon::parse(Auth::user()->created_at))->diffInDays(Carbon\Carbon::now()) }}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser5120193View Question on Stackoverflow
Solution 1 - PhpiivannovView Answer on Stackoverflow
Solution 2 - PhpCodeGuruView Answer on Stackoverflow
Solution 3 - PhpStephen SView Answer on Stackoverflow
Solution 4 - PhpthemrflibbleView Answer on Stackoverflow
Solution 5 - Phpmy bytecodeView Answer on Stackoverflow
Solution 6 - PhpErich GarcíaView Answer on Stackoverflow