21

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

Ratan Uday Kumar
  • 5,738
  • 6
  • 35
  • 54
Yuray
  • 747
  • 1
  • 9
  • 19
  • 2
    Not an answer to the OP, but if you are looking for how to change the timezone of e.g. zulu time input, it can be done like this `Carbon\Carbon::parse('2021-01-28T23:45:00.000000Z')->setTimezone('Europe/Brussels')->format('Y-m-d H:i')` => `"2021-01-29 00:45"` – PaulH Jan 28 '21 at 13:01

7 Answers7

38

Update file config/app.php

Eg: 'timezone' => 'Asia/Jerusalem' instead of 'timezone' => 'UTC'

Adam Pery
  • 1,924
  • 22
  • 21
  • 2
    i think this is the answer for latest Laravel, right?? `config('app.timezone')` will affect the `Carbon` timezone, right? can anybody confirm it? – Syamsoul Azrien Oct 09 '19 at 04:19
  • @Syamsoul Azrien Right – Adam Pery Jul 15 '20 at 11:30
  • 1
    I wouldn't recommend this IF you have deployed your application live already. Changing the timestamp globally means it will begin saving the timestamps in the database with the updated timestamp, meaning some rows of your databases will be saved with the old timestamp and others - with the new... – Borislav Itskov Nov 03 '20 at 15:25
22

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');
}
Hisham Shami
  • 439
  • 4
  • 8
  • 9
    On Laravel, a best practice is modifiying the `timezone`option from the `config/app.php` file. – cespon Feb 21 '19 at 01:57
21

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
}
GONG
  • 3,976
  • 1
  • 24
  • 27
  • 3
    That is an accessor not a mutator, but nonetheless I agree the best practice here (especially if your application spans timezones or is consumed in a timezone affected by DST changes) is to always save dates in UTC and just change the timezone when the date is displayed, not when it is stored. – BlueC Jan 04 '20 at 12:12
  • Thanks, this was the solution to my issue, as time zone was dependent on the user logged into the application. – Brec Jul 09 '21 at 16:02
16

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

thefallen
  • 9,496
  • 2
  • 34
  • 49
14

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');
}
Ashwani Garg
  • 1,479
  • 1
  • 16
  • 22
2

It looks like solution is to use not "CET" but one of explicit timezones, for example: "Europe\Minsk"

PHP Timezones

Timezones in Laravel 4

Yuray
  • 747
  • 1
  • 9
  • 19
-2

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()