96

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>
tereško
  • 58,060
  • 25
  • 98
  • 150
  • 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)`. – Thomas Van der Veen Sep 15 '16 at 10:55
  • Try With: `Carbon::parseDate($date);` insted of `createFromDate` – Filip Koblański Sep 15 '16 at 11:19
  • So i'm trying to calculate it via Blade: `{{Carbon\Carbon::now()->diffInDays($work['date']) }}` 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... –  Sep 15 '16 at 11:57

6 Answers6

188

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>
TylerH
  • 20,799
  • 66
  • 75
  • 101
iivannov
  • 4,341
  • 1
  • 18
  • 24
  • Well after many tries, this is the only way that works: ` {{ $diff = $datework->diffForHumans($now) }} ` It's all calculate directely the view layer, adding html comments code to remove the Blade output... –  Sep 16 '16 at 07:16
  • @MarcoFacc it's highly preferable to not do those calculations in the view template. – iivannov Sep 16 '16 at 11:30
  • 1
    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. –  Sep 16 '16 at 16:34
  • Carbon has built in Comparison, there's no need to go back to PHP. – themrflibble Jan 14 '21 at 23:08
  • That's exactly what's shown in the example - one of the Carbon's built in difference methods - `diffInDays()` – iivannov Jan 21 '21 at 16:24
  • FYI, Carbon's diffInX() methods default to now, so no need to specify if that's when you're comparing to. – Joel Mellon Jul 09 '21 at 17:16
  • 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. – Abdullah Iftikhar Nov 16 '21 at 08:05
15

Blade Template

A shorter code

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

Result : 6 minutes ago

CodeGuru
  • 3,645
  • 14
  • 55
  • 99
  • Is it possible to use if condition somehow. For example if ($diff > 10 ) print $diff else print 'short time ammount' – Crypcode Jul 15 '20 at 10:32
  • @wajih, not that i'm aware of but that's a good question, i do sometimes feel the need for something like that too. – CodeGuru Jul 16 '20 at 23:55
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>
Stephen S
  • 71
  • 1
  • 1
3

Shortest way

We can directly write it in blade

<span>{{ \Carbon\Carbon::parse( $start_date )->diffInDays( $end_date ) }}</span>
my bytecode
  • 404
  • 1
  • 4
  • 14
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.

themrflibble
  • 303
  • 3
  • 10
0

You can use on a Blade template directly:

{{ (Carbon\Carbon::parse(Auth::user()->created_at))->diffInDays(Carbon\Carbon::now()) }}
Erich García
  • 1,648
  • 21
  • 30