test131
test131

Reputation: 93

Carbon diffForHumans cant use in laravel 5.5?

first I use Carbon\Carbon; then I try to echo difforhumans reference by doc : https://carbon.nesbot.com/docs/#api-humandiff

echo Carbon::parse('2019-08-03')->diffForHumans('2019-08-13');

laravel 5.5 said Parse error: syntax error,

UPDATE

I install laravel 5.6 but I downgrade into 5.5

here is my full code

use Carbon\Carbon;
    public function index(Request $request)
    {
        dd(\Carbon\Carbon::parse('2019-08-03')->diffForHumans('2019-08-13'));

    }

enter image description here

Upvotes: 3

Views: 8634

Answers (4)

Mohamed Zaki
Mohamed Zaki

Reputation: 47

The basic way to do it like this:

dd(\Carbon\Carbon::parse('2019-08-03')->diffForHumans());

If you are retrieving a date from the database you can change it to be like this:

dd(\Carbon\Carbon::parse($user->created_at)->diffForHumans());

while user->created_at creates the date you get from tables.

Upvotes: 0

Munteanu Petrisor
Munteanu Petrisor

Reputation: 491

First of all diffForHumans don't take any arguments.If just removing the date from diffForHumans won't work..

Try this:

use Carbon\Carbon;
    public function index(Request $request)
    {
        dd(Carbon::createFromFormat('Y-m-d','2019-08-03')->diffForHumans());

    }

Upvotes: 2

Ashutosh Sharma
Ashutosh Sharma

Reputation: 432

Pass NULL in diffForHumans() and use Class Carbon

dd(Carbon::parse('2019-08-03')->diffForHumans());

Upvotes: 0

Adam Kozlowski
Adam Kozlowski

Reputation: 5896

That is the proper way:

echo \Carbon\Carbon::parse('2019-08-03')->diffForHumans(\Carbon\Carbon::parse('2019-08-13'));

The error you propably got was:

Argument 1 passed to Carbon\Carbon::diffForHumans() must be an instance of Carbon\Carbon, string given

It means that function "parse" needed proper Carbon data format.

Upvotes: 0

Related Questions