Reputation: 817
I have a project which is primarily based in CET region. I set CET in config/app.php, but all pivot timestamps in the base are stored in UTC time?
How can I set "global" timezone for timestamps?
i made 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's the result:
The current server timezone is: CET
06/09/2016 12:06:04 pm
2016-06-09 11:06:04
tnx Y
Upvotes: 24
Views: 120942
Reputation: 399
First Step change on config/app.php
'timezone' => 'UTC',
change to
'timezone' => 'Europe/Berlin',
create a function using Carbon
Carbon::createFromFormat('Y-m-d\TH:i:sP', $product->created_at)
->timezone(config('app.timezone'))
->toDateTimeString()
Upvotes: -2
Reputation: 1527
If you are using Laravel Carbon TimeStamps,
then you have to change timezone in App/Providers/AppServiceProvider.php
file
// App/Providers/AppServiceProvider.php
public function boot()
{
date_default_timezone_set('Asia/Calcutta');
}
Upvotes: 15
Reputation: 4026
You can achieve it with accessor
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
}
Upvotes: 22
Reputation: 2102
Update file config/app.php
Eg: 'timezone' => 'Asia/Jerusalem'
instead of 'timezone' => 'UTC'
Upvotes: 39
Reputation: 467
in the AppServiceProvider.php you can add the php functionality to alter the timestamp for the whole project
public function boot()
{
Schema::defaultStringLength(191);
date_default_timezone_set('Asia/Aden');
}
Upvotes: 22
Reputation: 817
It looks like solution is to use not "CET" but one of explicit timezones, for example: "Europe\Minsk"
Upvotes: 2
Reputation: 9749
Carbon uses the default DateTime PHP object, so use the date_default_timezone_set() function, for example:
date_default_timezone_set('Europe/London');
Upvotes: 16