How to update the time zone for timestamps (created_at and updated_at) that Laravel Eloquent manages?

I have a server in GMT, but I want to update these fields (* created_at * and * updated_at *) in order to save timestamps in PST.

I already did some tests trying to do this, but none of them were right.

  • I am updating config / application.php to 'timezone' => 'America / Los_Angeles
  • Also on the server, I changed the .timezone date in php.ini to 'America / Los Angeles

But any of these updates apply a temporary update to the Laravel Rloquent timestamps. These created_at and updated_at fields are Eloquent descriptors.

I also update the Ubuntu server timezone with sudo dpkg-reconfigure tzdata strong>, but this update does not work at all.

Edit: I need Laravel Eloquent timestamps to work on the PST timezone to insert futures into the database.

Any help would be helpful. Thanks.

+8
timezone php mysql eloquent laravel
source share
4 answers

I know this question is a bit outdated, but I came across it, trying to find the same solution, and wanted to share how I decided to solve it.

My advice is not to change the time zone in which the messages are stored. Store them in a database in UTC format. Keeping your vault in a constant reference system, and then converting it to any time zone you need, displays it for a long time.

As an example of one of these headaches, imagine that two people are trying to coordinate meeting times in different time zones where DST is observed, and one is not, and you need to display the time in each user local time. How difficult would it be to convert your stored PDT time to say: America / Cayman (which does not comply with DST)? And how will you consider when time is stored in PST and PDT? How do you know? (Hint: without probably hundreds of lines of additional code, you won’t be able to answer one question).

To get a timeout in the correct time zone, simply add the mutator function to the model itself:

use Carbon\Carbon; class MyModel extends Eloquent { // {{{ getCreatedAtAttribute() public function getCreatedAtAttribute($value) { return Carbon::createFromTimestamp(strtotime($value)) ->timezone('America/Los_Angeles') ->toDateTimeString() ; } // }}} } 

Now when you do $myModel->created_at , it magically converts to the correct time zone, but you still save the UTC in your database, which definitely has advantages over other time zones for permanent storage.

Want users to set their own time zones? Change the function as follows:

 public function getCreatedAtAttribute($value) { $user = Auth::user(); // If no user is logged in, we'll just default to the // application timezone $timezone = $user ? $user->timezone : Config::get('app.timezone'); return Carbon::createFromTimestamp(strtotime($value)) ->timezone($timezone) // Leave this part off if you want to keep the property as // a Carbon object rather than always just returning a string ->toDateTimeString() ; } 

And the whole complexity of changing time zones, taking into account savings or not, is distracted from you, and you can forget that this should even happen.

For more information on Laravel mutators / accessories, check out the documentation .

+14
source share

simple, just change the boot () function in AppServiceProvider.php

 public function boot() { //set local timezone date_default_timezone_set('Asia/Shanghai'); } 
+4
source share

I'm not quite sure that I understand if you want to change the server settings for future entries in the database or if you want to update the current values ​​in your database.

If this is the last, changing the settings of your server will not affect the current records in the database. If you want to change the values ​​of existing fields, you can do this with the MySQL command, for example:

  update `table` set created_at = (SELECT created_at) + INTERVAL 2 HOUR; 

Where table is the name of the database table, and 2 is the offset in hours.

+1
source share

For Lumen, this works in the .env file:

DB_TIMEZONE = "+ 03:00"

0
source share

All Articles