Carbon::isoFormat doesn't exist

Viewed 6394

since, laravel uses now() as the helper function for Carbon class, I tried this

dd(now('GMT+5:45')->isoFormat('x')))

but it returns laravel's bad method call exception saying that

Illuminate\Support\Carbon::isoFormat does not exist.

All I want is to convert date and time to time string just like strtotime() function

what sould I do now ?

2 Answers

It seems like you are using a version of Laravel which is lower than 5.8 where it includes Carbon 1, thus, isoFormat() is not included since it's part of Carbon 2.

So you may go with the option of using formatLocalized() but please note it's using a different placement pattern than isoFormat()

My advice to you is to upgrade to Laravel 5.8 if you want to use isoFormat()

If you would like to stick with your current version of Laravel and keep using formatLocalized() then you need to do the following:

  1. In one of your service providers, let's say AppServiceProvider, within your register() add: Carbon::setLocale($this->app->getLocale()); this is will localize ->diffForHumans() only.
  2. Help carbon by setting the php setLocal(), where it will use the already installed Locales in your machine to make sure your OS (Linux/Mac) has the desired local installed: you may check so by typing locale -a if it's not showing then run: sudo dpkg-reconfigure locales a simple GUI will appear to select the desired local by hitting the sapce button then enter, select the default Locale then.
  3. If you just installed the desired locales then please make sure to restart your php-fpm service: systemctl restart php7.3-fpm.service
  4. Use localized methods, in your case: ->formatLocalized(). i.g: ->formatLocalized('%A %d %B %Y')

You can use format() function from Carbon.

now('GMT+5:45')->format('y-m-d');

'y-m-d' could be replaced with format that you can see here.

EDIT: Or if you want to use isoFormat(),

You can use Carbon::now() instead of now().

Carbon\Carbon::now()->isoFormat();

This issue happens because now() is a Laravel helper and it returns Illuminate\Support\Carbon.

Related