How to check server timezone - mysql

I want to know what is the time zone that is currently set in the MySQL server. I do not have administrator rights to the computer I am using so I've tried the method by checking the registry.
I am doing a table with a timestamp column and I noticed the time stamped is different than the one on my computer's time. Is there any reason for this? How do I check what timezone it is on the MySQL server? How do I change it to match my local/computer's time?

You can set the timezone (if you know your offset) for the session by using
set session time_zone = '+00:00';
and to revert to the system default
set session time_zone 'SYSTEM';

In an SQL timestamp column, SQL automatically converts the time to UTC before storing it, using the session's current time offset. It will be the machine's time offset unless you change it (3). Depending on your server's settings (sql.ini), it may or may not always concert back to the expect timezone. This probably explains the time discrepancy.
To get the current timezone offset, try executing
SELECT ##session.time_zone;
To manually override the SQL timezone for the rest of a particular session, execute the following, replacing 00:00 with your desired offset:
SET ##session.time_zone = "+00:00";

Have a look at the system_time_zone system variable.

This may help:
http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
You can set the system time zone for MySQL Server at startup with the --timezone=timezone_name option to mysqld_safe. You can also set it by setting the TZ environment variable before you start mysqld. The permissible values for --timezone or TZ are system dependent. Consult your operating system documentation to see what values are acceptable.

You can convert a given timestamp to UTC (or any other TZ you want) with CONVERT_TZ
SELECT CONVERT_TZ(NOW(),##session.time_zone,'GMT');
Note that I use NOW() as simple demonstration, you would put in the timestamp you wanted to convert.
By the same token, you could convert a timestamp in your local TZ to the system
SELECT CONVERT_TZ($timestamp,'Your Time Zone' ##session.time_zone);

To check your shared server
<?php
echo date_default_timezone_get();
?>
To change
<?php
date_default_timezone_set("Africa/Addis_Ababa");
echo date_default_timezone_get();
?>

Related

Lumen show different time than what is saved is MYSQL DB

my current timezone is Asia/Karachi and when i retrieve table data from mysql it gives me (actual time - 5 hours)
for eg:
mysql column value: '2021-04-21 01:34:57'
and when i retrieve from laravel DB::table('table_name')->get()->toArray();
it gives following :2021-04-20 20:34:57
and changing my timezone doesn't change anything either.
so is there something else i'm missing ?
btw i created following route for checking my current timezone
$app->get('/timezone', function () {
return date_default_timezone_get();
});
and it gives same what is saved in my env i.e(Asia/Karachi) but this doesn't change the result i get from mysql even if i change it to some other timezone like Asia/Kolkata.
I tried researching on this but didn't get any suitable answer.
i tried alot of things to solve this issue like
adding env variables APP_TIMEZONE="Asia/Karachi" (did not work)
also i tried adding DB_TIMEZONE="+05:00" (this worked in my local but not on stagging)
finally i saw somewhere that someone else had exact same issue resolving this and he did it by adding the hours using carbon which also worked for me
'posted_at' => Carbon::parse($record->posted_at)->addHours(5)->toDateTimeString(),
i know this is not the optimal solution but this was the only solution that worked so i had to go with it.
https://dev.mysql.com/doc/refman/8.0/en/datetime.html
If you store a TIMESTAMP value, and then change the time zone and retrieve the value, the retrieved value is different from the value you stored. This occurs because the same time zone was not used for conversion in both directions. The current time zone is available as the value of the time_zone system variable.
As far as I know, you have a couple things to consider:
The timezone of your Laravel/Lumen app
This can be found found in config/app.php -> timezone.
The timezone of your MySQL server
You can retrieve this using:
mysql> SELECT ##global.time_zone, ##session.time_zone;
The timezone of the server itself
If the mySQL query above returns SYSTEM, it means it uses the system timezone setting, which for Debian/Ubuntu etc you can check using:
cat /etc/timezone
From my experience, you can most often leave the system/mySQL timezones intact and only set the correct timezone in your Laravel config. I know it caused me a headache the first time I had to figure out how this actually worked.

Bypass MySQL automatic GMT conversion when selecting TIMESTAMP fields

There's lots of stuff on the internet about converting MySQL timestamps, how it works etc. But what I need is the opposite: knowing that MySQL stores every datetime data as UTC in TIMESTAMP fields, all I want is to direclty retrieve the stored UTC value without MySQL messing around the datetime with system/server/connection timezones.
You see, we've built a simple node.js feeder which reads from several third-part MySQL databases (so I can't change their timezone settings) and save the gathered data to a Elasticsearch, as a "denormalization process". As the original data comes from different timezones, I need to store them in UTC, so I can easily coordinate further GETs and aggregations.
I know I can set the connection timezone on the fly and I know I can change every timestamp field fetched in my node application, but since MySQL engine already stores timestamps in UTC, why should I add any other step if I could simply get it directly, without converting functions or costly data processings?
In a nutshell, I'd like to know: is there a way to bypass MySQL automatic GMT conversion?
MySQL provides a UNIX_TIMESTAMP function which returns a raw integer value. And that isn't subject to timezone conversions at all.
SELECT UNIX_TIMESTAMP( timestamp_col ) ...
But that returns a raw integer, not a datetime. The client would need to do the conversion into a "datetime" type object, if that's needed.
Another option would be to use the CONVERT_TZ function to convert to UTC from the session time_zone setting.
SELECT CONVERT_TZ( timestamp_col, ##session.time_zone,'+00:00')
But, that doesn't really "bypass" timezone conversion. One downside of this approach is if the session time_zone is affected by daylight saving time changes, there's ambiguity with a one hour period each year when the clock "falls back" one hour. e.g. Sunday, Nov 1 2015 2AM CDT transition to Sunday Nov 1 2015 1AM CST. (Converting back from UTC, if we get 1:30 AM in the session time_zone, we don't "know" if that's CDT or CST. And the conversion back to UTC doesn't know which it is either.)
Another option (which I think you already mentioned) is changing the session time_zone to UTC. Then you could just return the timestamp_col value as UTC. You could save the current time_zone setting, and set it back when you are done, e.g.
SET #save_session_time_zone := ##session.time_zone ;
SET time_zone = '+00:00' ;
SELECT timestamp_col ...
SET time_zone = #save_session_time_zone ;
But your client Connector might do some not-so-helpful conversions when the time_zone of the MySQL database session doesn't match the time_zone of the client, like the funky shenanigans the JDBC driver (MySQL Connector/J) does. (That concern isn't limited to returning UTC; that's a concern whenever the time_zone of the client doesn't match the time_zone of the database session.)
It looks like there's no way to get the original UTC value from a MySQL field; every single function uses the timezone setting, be that SYSTEM or any other you configure.
The way MySQL forces you to use a date conversion is, at least, very constraining. For example, say you have a MySQL server set to a timezone with GMT -03:00 and GMT/DST -02:00 and you store a datetime like '2016-07-01 10:00:00'. If you select this value after the DST has ended, you'll get '2016-07-01 09:00:00'.
You can't tell what time it is for sure unless you store the GMT offset separately or you previously know what timezone the server was when it was stored.
We used the second approach. We saved the server timezone and used it to calculate the offset and return an ISO datetime, so future calculations can be made easily.
DROP FUNCTION IF EXISTS `iso_datetime`;;
CREATE FUNCTION `iso_datetime` (
p_datetime TIMESTAMP
) RETURNS VARCHAR(25)
READS SQL DATA
BEGIN
DECLARE _timezone VARCHAR(255) DEFAULT NULL;
DECLARE _offset VARCHAR(6) DEFAULT NULL;
SET _timezone = (SELECT timezone FROM network);
SET _offset = (SELECT SUBSTRING(TIMEDIFF(p_datetime,CONVERT_TZ(p_datetime, _timezone,'UTC')), 1,6));
RETURN CONCAT(DATE_FORMAT(p_datetime, '%Y-%m-%dT%H:%i:%S'), _offset);
END;
In order to do so, you have to load timezone info into MySQL, so the server can calculate the tz offset of the date for you.

Change timestamp in mysql

I lived in philippines and when I'm inserting date in my database, It's inserting less 13 hours in my database. For example, our datetime here is 02-11-2016 21-04-00, when I insert, it's inserting 02-11-2016 08-04-00
I already used this:
date_default_timezone_set('Asia/Manila');
But no luck. I already put the website in my domain.
This should be done by
date_default_timezone_set('Asia/Manila');
Make sure you are uploading the correct files.
If it doesn't work; try making a page for error handling, then echo a variable that will set the time for this timezone, if it works. Use that variable instead.
The timezone you set is used for PHP as Bimal said, you need to check the server time as well, because time is very sensitive part of the information you don't want to miss or play with.
I've been in that situation couple of weeks back, there are two ways to fix it:
Set the server timezone to the correct timezone.
If the time difference is always and EXACTLY 13 hours, use this approach:
$new_time = date("Y-m-d H:i:s", strtotime('+13 hours')).
I ended up using the second method as the first one was not an option for me.
Best of Luck.
If I am not wrong you have pasted php function which will just set time_zone for php not for my_sql. mysql has global time_zone variable which can be set. You should use SET GLOBAL time_zone = timezone; or SET time_zone = timezone;.
If you dont have direct control of your DB server in that case you can pass the 2nd statement i.e set time_zone=...; as 1st statement.
Alternatively you can also use the convert_tz function to convert an already stored value.

mysql server_time_zone or time_zone

am trying to change the time of my sql server:
serve_time_zone=GMT Daylight Time
time_zone=SYSTEM
i have tried:
SET TIME_ZONE='+01:00';
to get the correct time_zone, but when i restart server to take effect it resets itself.
i have searched and not found anything which will not reset itself. I have now downloaded Hiedi SQL to view my database.
I want to advance the time of the database by 1 hour 1+GMT
Have you tried this: SET GLOBAL time_zone = timezone;??

Setting default-time-zone on MySql Server via PhPMyAdmin

I have a shared hosting mySql instance which has it's system_time_zone set to Pacific Standard Time and it's time_zone variable set to System, hence effectively it's running on Pacific Standard Time.
i.e. I've run the following command to find this out:
SELECT version( ) , ##time_zone , ##system_time_zone , NOW( ) , UTC_TIMESTAMP( )
I would like to change the default mySql database / local mySql DB time-zone to GMT/UTC time. I tried to run, SET time_zone = '+0:00', and this does execute successfully!
However, this does not seem to affect the time_zone variable, when I check the state of ##time_zone. I've looked at another post dealing with similar issue How to set MySQL to use GMT in Windows and Linux and I also checked the MySql documentation, with little progress. Since I am on a shared-hosting solution, I have limited access and I don't have access to more than what my PhPMyAdmin mySql functionality has on offer.
I wonder if there is any way to change the default_time-zone from within an SQL query, or do I need to fall back to the command line (to which I don't have access to, unfortunately).
Thanks for your help and advice,
Martin
In short, MySQL actually stores 'datetime' data type fields internally as UTC.
However, PhpMyAdmin shows you the dates using the server default time, hence your confusion.
For example, try adding this line before your SQL statement in PhpMyAdmin:
SET ##session.time_zone='+00:00';
SELECT * FROM MY_TABLE
See the MySQL documentation for further details, or the answer in this post: How to correctly set mysql timezone
Cheers
Matt
For shared hosting, you have to ask support-guys to help you and change default time zone for you? I had similar problem with Arcor hosting-provider, called them and they fixed it. Before that, I found temporary solution in date_default_timezone_set() from PHP code. Probably the best solution is to ask someone who has privilege to change that parameter.
<?php
date_default_timezone_set('UTC'); //define local time
$date=date('l jS \of F Y h:i:s A'); //type of time shown
$conn=mysql_connect("localhost","root","") or die('Could not connect!'); //your database connection here
$db_selected = mysql_select_db('databasename', $conn); //select db
$result=mysql_query("INSERT INTO table (date) VALUES ('$date')", $conn);
?>
Simply sent the time as VARCHAR into db hope it helps and sorry for syntax errors (if there are any).