Home > Net >  How can I overwrite timestamp to save into the database base on specific timezone?
How can I overwrite timestamp to save into the database base on specific timezone?

Time:01-23

Right now my Laravel application save() any items into the database base on this timestamp. America/New_York because I configured it as 'timezone' => 'America/New_York', in config/app.php.

Goal

I wish to overwrite timestamp based on other tz instead, ex. America/Chicago

How do I do that?

CodePudding user response:

You don't need too

MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.)

see https://dev.mysql.com/doc/refman/5.7/en/datetime.html

So when you change the server to another timezone all timestamps will get the new time zone

setting the time zone goe like

SET GLOBAL time_zone = 'America/Chicago';

CodePudding user response:

You don't have to do crazy stuff... What you have to do is:

  • Store the time in a known timezone and never change that timezone again, it would be awesome if you use UTC as a default timezone.
  • When you want to "convert" a timezone, you just $model->created_at (or anything that is a Carbon object) and do $model->created_at->setTimezone('America/Chicago'); (for example).

The main idea is that when you already have a Carbon instance with a timestamp, you just change the timezone with setTimezone to the new one you want and it will return a new Carbon instance with that timezone...

Have a look at this SO topic.

Also, remember that timestamp is just an integer representing how many seconds have passed since 1970-01-01 00:00:01 (UTC), so if you say "give me a timestamp of a specific date and time on specific timezone" the timestamp will be always the same even if you change the timezone each time... that is the main idea of the timestamp...

If I say "What timestamp is for 1970-01-01 00:00:10?", if you are on UTC you would get 10, because 10 seconds passed since that specific datetime, if you are on UTC 1, it would still be 10 seconds, but you will display 1970-01-01 00:01:10, because you are 1 hour ahead of UTC, if you are on UTC-1 it will be 1969-12-31 23:00:10, because you are 1 hour behind UTC, but you know how to do the conversion, that is why the value will be always the same disregarding the timezone, and that is also why it is 1970-01-01 00:00:00 and not any other specific datetime, because if you do not know which is the specific datetime you would not know how to do the conversion.

It is very important that you understand what you are working with, so to help you understand better, have a look at this blog explaining the same thing but in more detail.

CodePudding user response:

The timestamp generated in PHP is in UTC time regardless of your local timezone setting.

You can adjust the timezone of the timestamp before it is displayed in the UI using setTimezone.

Reference:

https://www.php.net/manual/en/datetime.gettimestamp.php

  •  Tags:  
  • Related