Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

majacirkova's avatar

Ho can i display date time with Carbon in my local time ?

Ho can i display date time with Carbon in my local timezone ? I m using this code in my view to display time : {!! $mytime = Carbon\Carbon::now(); !!}

But it displays time : 2016-10-18 09:30:04

Currently in my city is: 2016-10-18 11:30:04

How can i make to display timezone depends from the user location ?

Thanks in advance

0 likes
9 replies
gabrielbuzzi's avatar

In you AppServiceProvider on boot method use

Carbon::setLocale('en');
majacirkova's avatar

I put it : Carbon::setLocale('en'); on boot() function and also in config/app is set to: 'timezone' => 'UTC', still give me time 09:47 , in my place is :11:47 ?

katifrantz's avatar

You could set the locale using PHP's setLocale() function as such :

//get locale from laravel-config-file
setlocale(LC_TIME, config('app.locale'))
//or set locale for your location
setlocale(LC_TIME, 'en')

You can add this function in your register() method of AppServicePorvider, so it gets loaded in the laravel lifecycle

Edit ::

You have to use the Carbon formatLocalized($format) method to get the desired results.

gabrielbuzzi's avatar

Try this

app/providers/AppServiceProvider.php

public function boot() 
{
    Carbon::setLocale(config('app.locale'));
}

config/app.php

'locale' => 'en',
majacirkova's avatar

@gabrelbuzzi i try it , but the same not shows me the correct time , it is shows time that is forward 2h. But in config/app when i change timezone to be "Europe/Skopje" it is working , but i don't want to work only for Skopje , i want to work depends where user is.

gabrielbuzzi's avatar

@majacirkova Hmm, got it, but this the set_locale get the timezone from server side, maybe you should try to get the client locale and the in the config/app.

i don't know if this will works, but searching a little bit i found this Server Variable

'locale' => $_SERVER['HTTP_ACCEPT_LANGUAGE'],
socialmosaic's avatar

sorry for late but i am stuck in this problem yesterday and find a best solution for this

In helpers.php

function get_local_time(){

   $ip = file_get_contents("http://ipecho.net/plain");

   $url = 'http://ip-api.com/json/'.$ip;

   $tz = file_get_contents($url);

   $tz = json_decode($tz,true)['timezone'];

   return $tz;

}

in controller

 dd(carbon::now(get_local_time()));

it will find client ip address and it return an json object in it gives us client timezone.

Cronix's avatar

@socialmosiac Wouldn't that return the info/timezone for the server, and not the individual user accessing the server?

Please or to participate in this conversation.