mardi 29 octobre 2019

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

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.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/32YfxFH
via IFTTT

Aucun commentaire:

Enregistrer un commentaire