Calculate difference between two dates using Carbon and Blade
Asked Answered
B

6

97

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>
Bell answered 15/9, 2016 at 10:37 Comment(3)
What is exactly the problem you are trying to explain. Do you get any errors? Does it show the wrong values? I think your solution will be using new Carbon($date) instead of Carbon::createFromDate($date).Lavallee
Try With: Carbon::parseDate($date); insted of createFromDateNaucratis
So i'm trying to calculate it via Blade: <td>{{Carbon\Carbon::now()->diffInDays($work['date']) }}</td> but i have this error: Type error: Argument 1 passed to Carbon\Carbon::diffInDays() must be an instance of Carbon\Carbon, string given. So i have the variable $work['date'] that is a result from the Model query and not a Carbon Object...Bell
X
191

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>
Xerosis answered 15/9, 2016 at 20:33 Comment(7)
Well 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...Bell
@MarcoFacc it's highly preferable to not do those calculations in the view template.Xerosis
ideally 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.Bell
Carbon has built in Comparison, there's no need to go back to PHP.Oralla
That's exactly what's shown in the example - one of the Carbon's built in difference methods - diffInDays()Xerosis
FYI, Carbon's diffInX() methods default to now, so no need to specify if that's when you're comparing to.Corrupt
This 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.Vile
W
16

Blade Template

A shorter code

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

Result : 6 minutes ago

Withers answered 24/10, 2018 at 15:31 Comment(2)
Is it possible to use if condition somehow. For example if ($diff > 10 ) print $diff else print 'short time ammount'Dietsche
@wajih, not that i'm aware of but that's a good question, i do sometimes feel the need for something like that too.Withers
B
7

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>
Biggs answered 26/9, 2018 at 21:59 Comment(0)
L
3

Shortest way

We can directly write it in blade

<span>{{ \Carbon\Carbon::parse( $start_date )->diffInDays( $end_date ) }}</span>
La answered 5/6, 2020 at 18:41 Comment(0)
O
3

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.

Oralla answered 14/1, 2021 at 23:16 Comment(0)
P
1

You can use on a Blade template directly:

{{ (Carbon\Carbon::parse(Auth::user()->created_at))->diffInDays(Carbon\Carbon::now()) }}
Pantheon answered 22/2, 2022 at 18:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.