how timestamp's timezone works in a database and/or Laravel

wmfairuz

I have a question regarding timezone for timestamps.

How the timezone in app.php is used? I realized that the behavior is different if I create a timestamp using Carbon or if I query the value (timestamp) from the DB.

Note: My MySQL use system's timezone which is GMT+8 or Asia/Kuala_Lumpur.

  1. Example #1
    • Set timezone="UTC" in app.php
    • Create a Carbon instance
>>> new Carbon\Carbon;
=> Carbon\Carbon @1572404830 {#3496
     date: 2019-10-30 03:07:10.625282 UTC (+00:00),
   }
>>>
  1. Example #2
    • Set timezone="Asia/Kuala_Lumpur" in app.php
    • Create a Carbon instance
>>> new Carbon\Carbon;
=> Carbon\Carbon @1572404816 {#3520
     date: 2019-10-30 11:06:56.316851 Asia/Kuala_Lumpur (+08:00),
   }

For example #1 and #2, this is for me, expected. You got different value based on the timezone. Things got a little weirder (at least for me), when we query a timestamp from the DB.

  1. Example #3
    • Set timezone="UTC" in app.php
    • Query from DB
>>> RefCyberCity::whereDataSource('ccms')->take(1)->first()->updated_at
=> Illuminate\Support\Carbon @1572083605 {#3531
     date: 2019-10-26 09:53:25.0 UTC (+00:00),
   }
  1. Example #4
    • Set timezone="Asia/Kuala_Lumpur" in app.php
    • Query from DB
>>> RefCyberCity::whereDataSource('ccms')->take(1)->first()->updated_at
[!] Aliasing 'RefCyberCity' to 'App\RefCyberCity' for this Tinker session.
=> Illuminate\Support\Carbon @1572054805 {#3491
     date: 2019-10-26 09:53:25.0 Asia/Kuala_Lumpur (+08:00),
   }

We can see that both output 2019-10-26 09:53:25.0 but the timezone is different.

Thomas Edwards

Dates coming from the database are effectively a string, and although MySQL has its own timezone information, that is not reflected in a date string without a timezone in it.

The Laravel DB drivers are asuming that it was written as the timezone that’s set in the app config. If you were to change your timezone mid-project, this would throw out all your dates.

My personal advice on timezones is to keep everything as UTC on your application and servers, then only change the timezone at the last possible moment (such as in the view), based on the user’s preference. This also allows you to output the UTC timestamp and have JavaScript update it to the user’s locale. Timezones are very hard, and if your country has daylight savings, timezones become even more confusing as Europe/London for example shifts timezone depending on the year.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Oracle how to convert Timestamp with any Timezone, to Date with database server Timezone

Laravel set timestamp timezone

How to guess the timezone of a timestamp?

How to configure Code Workbook's timezone for CURRENT_TIMESTAMP?

How to convert timestamp string to timestamp in another timezone

how to get the timestamp with timezone in rust

How is a timestamp with timezone read in PostgreSQL?

how to display timestamp without timezone

How to handle JodaTime's and Android's timezone database differences?

Laravel convert timestamp into date with specific timezone

How to get user's timezone and convert time saved in database according to the user's timezone? Please see details

How to set the timezone of the database

What's a good way of converting timezone of a timestamp?

Create an integer timestamp that corresponds to a Pandas Timestamp's timezone

How to substract timestamp without timezone in PostgreSQL

How to set Timezone for DynamoDBAutogenerated timestamp in spring data

How to convert a string to timestamp in a desired timezone

how to convert a timestamp into string (without changing timezone)?

How to convert unix timestamp (milliseconds) and timezone in R?

How to convert a string with timezone to unix timestamp python?

How to set a timezone to an existing timestamp without reinterpreting it?

How to add/subtract TimeZone Offset to a Timestamp in Java?

How to change timezone in Winston timestamp ? Node js

How to change lightsails database timezone

How to convert a timestamp varchar that contains a timezone to a timestamp type

Which timezone is selected when Timestamp is put in StackOverFlow database

Java Tomcat Application is Saving Timestamp to the Database in Wrong Timezone

Best practice for maintaining current Timezone in Database - Laravel

How to set timezone in ActiveSupport's TimeZone class