Laravel 5 timestamp not updating at the right time

After inserting a record to the database, the created_at

timestamp is not displayed at the right time, it is delayed by 2 hours from the server .... Also, if I type mysql SELECT NOW()

, it is displayed at the right time, any idea what the problem is?

Edit ..

Requires date from class Carbon

... any idea how to change it?

+3


source to share


2 answers


The default timezone

for laravel is UTC

, which is in the file config/app.php

.

If you want to change timezone

to preferred timezone

, select your preferred timezone

from this list or from this list and replace UTC

with your chosen time zone.



A few notes. As per the comments here , the last 3 comments to be precise: you shouldn't change the defaults.

Storing dates different timezones

in the datasource of the same application (by changing timezone

the config for the current user & letting Laravel handle it from there) just asks about problems & bad design. You will lose data integrity. What happens when custom changes timezone

? Will you update all the dates in the database for this user?

Store dates for all users as UTC (or whatever time zone you choose, just pick one and stick with it). Laravel already uses the excellent Carbon library , use it to convert dates from your timezone

(where they are stored in the DB) to users timezone

(which you will store in every user setting) when you display dates.

+5


source


For other people who also still have the wrong date / time after modifying the config file:

My problem was php in my vagrant field (Homestead).

To solve this problem I did:

First enter ssh into the firewall window and then start it from the command line:

date

      

This should return something like "Mon Jul 3 13:48:52 CEST 2017". Check if this date / time is correct. If you don't do the following:



sudo ntpdate -b pool.ntp.org

      

This should update the system time. Check it again with the first command. If it is still not written, you may have to change your system's time zone. You can do this by running:

sudo rm -f /etc/localtime

sudo ln -sf /usr/share/zoneinfo/Europe/Amsterdam /etc/localtime 

      

You can use any time zone you like, I prefer "Europe / Amsterdam".

You can get other time zones here

+1


source







All Articles