Overcoming timezone differences in MySQL Select - mysql

I am trying to select entries for a current date but cannot seem to get past the issue of a 1 hour time difference between my timezone and that of the server.
I was able to overcome this by using DATE_ADD() for adding entries but now I need to do the same to SELECT them. I tried inserting SET time_zone = 'America/New_York'into my MySQL connection command but it did not appear to change anything. I am using a PDO::QUERY statement, here is my connection command:
$db = new PDO('mysql:host=localhost;dbname=nightdes_points', $dbuser, $dbpsw);
What am I missing here? Thanks!

I would always store data in GMT0 and on insert/update/select I would always pass (converted) GMT0 date.

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.

Wrong timestamp in MySQL

I make an insert using now() as default, and when i look in the table it says for example 11:00, and when i click on it, it specifies the timezone (+3:00). Which means that it equals 8:00 UTC+0. Which is WRONG, because I actually made that insert at 11:00 UTC+0.
More strangely, when I try "SET time_zone = '+9:00'" or no matter which timezone I specify, it doesn't change ANYTHING - now() still creates the wrong timestamp with with UTC+3.
If I just write a single query "SET time_zone = '+3:00'; SELECT now()", it returns the correct value.
But if I write "SET time_zone = '+3:00'; update table set time=now() where id=11", and then check the table, the problem appears again.
I'm using 000webhost.
Please help? It drives me insane.

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).