How to set Laravel time range for timestamps?

I have a project that is mainly based in the CET region. I installed CET in config / app.php, but are all the timestamps in the database stored in UTC?

How can I set a “global” time zone for timestamps?

I did this test:

<?php $timezone = date_default_timezone_get(); echo "The current server timezone is: " . $timezone; echo "<br />".date('m/d/Y h:i:s a', time()); $mytime = Carbon\Carbon::now(); echo "<br />".$mytime->toDateTimeString(); ?> 

and here is the result:

 The current server timezone is: CET 06/09/2016 12:06:04 pm 2016-06-09 11:06:04 

TNX Y

+9
source share
6 answers

Carbon uses the default PHP object DateTime , so use the date_default_timezone_set () function, for example: date_default_timezone_set('Europe/London');

+14
source

in AppServiceProvider.php you can add php functionality to change the timestamp for the whole project

 public function boot() { Schema::defaultStringLength(191); date_default_timezone_set('Asia/Aden'); } 
+7
source

You can achieve this with mutators.

 public function getCreatedAtAttribute($value) { return Carbon::createFromTimestamp(strtotime($value)) ->timezone(Config::get('app.timezone')) ->toDateTimeString(); //remove this one if u want to return Carbon object } 
+3
source

It seems that the solution is to use not “CET”, but one of the explicit time zones, for example: “Europe \ Minsk”

Php timezones

Time Zones in Laravel 4

+2
source

Update config/app.php

For example: 'timezone' => 'Asia/Jerusalem' instead of 'timezone' => 'UTC'

0
source

If you use Laravel Carbon timestamps, you need to change the time zone in the file App / Providers / AppServiceProvider.php

//App/Providers/AppServiceProvider.php

 public function boot() { date_default_timezone_set('Asia/Calcutta'); } 
0
source

All Articles