Laravel 5 timestamp not update right time - mysql

After inserted record into database, created_at timestamp not display right time, it late 2h from server.... Also If i type into mysql SELECT NOW() it show right time, any idea what is problem?
Edit..
It take date from Carbon class... any idea how to change it?

The default timezone for laravel is UTC which is located in config/app.php file.
If you want to change the timezone to your preferred timezone, choose your preferred timezone from this list or from this list and replace the UTC with your chosen timezone.
A few notes. As per the comments here, to be precise, the last 3 comments: You should not change the default values.
Storing dates of different timezones in data source of a same
application (by changing the timezone in config for current user &
letting Laravel handle it from there on) is just asking for trouble &
is a bad design. You will lose data integrity. What will happen when a
user changes timezone? You'll update all the dates in the database for
that user?
Store dates for all users as UTC (or any other timezone of your
choosing, just select one & stick to it). Laravel already uses the
excellent Carbon library, use it to convert dates from your
timezone (in which they're stored in DB) to users' timezone (which
you would store with in every user's settings) when you display the
dates.

For the other people that also still have the wrong date/time after changing the config file:
My problem was in php in my vagrant box (Homestead).
To solve it I did:
First ssh into you vagrant box and then run this in 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 not do the following:
sudo ntpdate -b pool.ntp.org
This should update your system time. Check it again with the first command. If it is still not write you probably have to change your systems timezone. 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 timezone you wish, I prefer "Europe/Amsterdam".
You can get other timezones here

Related

Stop changing dates automatically in MySQL on tz change

I am using DATETIME field to store my dates in MySQL. Without any timezone specified directly (there is just a datetime column)
But when timezone was changed on server MySQL updated all datetime columns according to new timezone.
Actually switching to EDT was the reason.
I don't need to recalculate my dates automatically - just want to store specific dates in it.
So even if tz changed manually to UTC from EST date should be same (from characters POV) if it was 2016-01-01 18:55 it should be same in any new tz..
I did not run any scripts\queries to update dates.
So it was performed either by MySQL itself or by server.
Need advice what I need to check to find and disable such feature.
Make sure you are using DATETIME and not TIMESTAMP
[From the MySQL documentation][1]:
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.)
You should also review this post (Duplicate):
Will changing a MySQL timezone change values of DateTime fields in a database?
I am sorry for the mess I brought
Just extended my query to grab some old dates - and it looks unchanged
So error defenetly in my code..
Anyway - thanks for your help

Convert all datetime fields to UTC

I've build a web app which is currently being used by a number of customers.
In v1.0 it didn't support time zones and stored all datetimes in Europe/Amsterdam.
Now in v1.2 it stores all datetimes in UTC and shows the right date in the web app according to the user's selected time zone.
Now I want to provide a mysql query to my customers (who are updating and already have some data) to update all datetime fields to UTC in phpmyadmin.
I'm using CakePHP so all created/modified fields need to be updated.
Can anyone show me what this query looks like?
You can use the timezone conversion features provided by MySQL itself:
-- "table" and "field" are obviously placeholders
UPDATE table SET field = CONVERT_TZ(field, 'Europe/Amsterdam', 'UTC');
Be sure to make a backup first.
Note that CONVERT_TZ requires MySQL to be aware of the timezones. You can import them to the database with a simple shell command (requires root access to the DB):
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p -D mysql
This can be done multiple times, so it doesn't hurt to execute it if you don't know if the timezones have already been imported. You can usually ignore warnings concerning some obscure timezones.

Setting MySQL time_zone info without shell access

I am working on a database which contains a number of "Date" fields. These dates need to be converted to UTC for some output, which I intend to do using
CONVERT_TZ('theField','GMT','UTC').
However, the ##global.time_zone variable is not currently set (returns "SYSTEM" at present), and the timezone information table is currently empty. I believe this needs to be filled by running the mysql_tzinfo_to_sql shell script (http://dev.mysql.com/doc/refman/4.1/en/time-zone-support.html).
My problem is that I don't have any kind of shell access to the system in question, so it's impossible for me to run the mysql_tzinfo_to_sql script. So I therefore have two questions...
1) If MySQL is using the "SYSTEM" time zone at the moment, is there any way to determine exactly what the system time zone is (remembering I don't have shell or any other access)?
2) Is it possible to generate the information in the MySQL time zone table by some means other than the shell script, i.e. entirely within MySQL itself?
Grateful for any suggestions, thanks.
You can check what time zone you are using with:
SELECT ##global.time_zone, ##session.time_zone;
Setting the timezone:
SET ##global.time_zone = 'SYSTEM';
If you want to know the time zone of the system:
Windows:
Go to cmd and key in systeminfo
Linux:
date +%Z

Set MySQL database timezone to GMT

I need to change the timezone of a single database is this possible?
I know we can change the timezone within our WHM (we are using a dedicated server from hostgator), however a large number of legacy software running on the server has a lot of +6 hours coding in it (i.e. server timezone is CST, we wanted the time in GMT so previous developers altered date/times manually within the code - bad!).
I am now working on a new software and would like to have it all in GMT, I know I can use date_default_timezone_set('GMT') however that will not solve MySQL inserts where the datetime column is set to CURRENT_TIMESTAMP as it will insert # CST timezone.
No, it's not possible to change the timezone for a single database within a MySQL instance.
We can retrieve the server and client time_zone settings with a query, like this:
SELECT ##global.time_zone, ##session.time_zone;
We can also change the client timezone for a session, or change the timezone for the entire MySQL instance.
But we need to be keenly aware of the implication that this change will have on existing client connections, and the how DATETIME and TIMESTAMP values already stored in the instance will be interpreted.
To have the server time_zone set at MySQL instance startup, we can modify the /etc/my.cnf file (or wherever the mysql instance initialization parameters are read from), under the [mysqld] section:
[mysqld]
default-time-zone='+00:00'
-- or --
It is also possible (less desirable) to add the --default_time_zone='+00:00' option to mysqld_safe
NOTE: Changing the timezone setting on the MySQL server does NOT change the values stored in existing DATETIME or TIMESTAMP columns, BUT since it does effectively change the context in which those stored values are interpreted, it will look like all of the values ARE shifted. (Where 08:00 was taken to mean 8AM CST, with the time_zone of the server changed from CST to GMT, that same '08:00' will now be taken to be 8AM GMT, which would effectively be 2AM CST.
Also keep in mind that TIMESTAMP columns are always stored in UTC, while DATETIME columns do not have a timezone.
http://dev.mysql.com/doc/refman/5.5/en/datetime.html
Each client session can change the timezone setting for their own session:
SET time_zone='-06:00';
But none of this really "solves" the timezone conversion problem, it just moves the conversion problem around.
There's nothing inherently "bad" with the application layer handling timezone conversions; sometimes, that's the best place to handle. It just has to be done correctly and consistently.
(What's odd about the setup you describe is that the app is storing DATETIME values as if the MySQL server time_zone is set to GMT, but the MySQL server time_zone is set to something else.)
If you can't change your current time zone you can change the result.
date 2015-01-05 12:06:16
Select date + Interval 2 Hour 'date' From ExampleTable
date 2015-01-05 14:06:16
You can modify the the default value instead of entering current_timestamp make it insert current_timestamp added to hours offset of your timezone. I just did it this way when didn't found any solution, had to invent my own.

MYSQL wrong time.now

We got a Rails 3 Server based on Ruby 1.9.2 running on a MYSQL DB and Problems with the time.now and time.zone.now setting.
For better explaination we assuse its 13:00 UTC right now.
Ubuntu Systemtime is set to 15:00 UTC +02:00 (Thats CEST (Central European Summertime) and is correct for our Timezone).
The Rails time (time.now) is set to 13:00 +0200
the time.zone.now is set to CEST
MYSQL Global time and Session time are both set to SYSTEM
now we create a new entry in the MYSQL DB (via activerecord) which has a timestamp.
but when we look at the timestemp its -2 hours offset to the current server time (in our example 11:00)
We tried setting the time.zone to a very different one for testing propose, it had absolutly no effect on a new mysql entry.
Also we tried to set the mysql global time and session time to UTC, no effect.
The only thing we can imagene right now what could be the problem is that the time.now is taking effect on the mysql entry and is set wrong. Problem here: we're unable to set the time.now setting. But even if that would be possible somehow (is it?), dunno if that fixes the problem.
Did anyone ever have such a problem or any ideas what could cause it?
There are two things to configure for rails.
The current timezone which you've done (used in Time.zone.now), this sets the timezone for timestamps in the instantiated records you load.
The timezone that those values are converted to when stored to the db. This should be set to the same timezone as your system/mysql.
I think this option is configured via: ActiveRecord::Base.default_timezone = :utc
Since your system time is set to CEST, you'll want to use the same value for that as well.
The reason there are two config options is incase you've got users in multiple timezones (international audience), then Time.zone can be set differently for each logged in user, and the db timezone will convert correctly to the Time.zone timezone.