Setting timezone for MySQL - php

Im using php to set the current timezone for a site:
date_default_timezone_set('America/New_York');
How could I, at the same time, set the same time zone for mysql so that when I use NOW() it uses the timezone set in php?

Don't.
The best practice is to store UTC in the database.

You can change your Mysql timezone using SET time_zone = timezone;
http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html

Related

Set timezone for MYSQL NOW()

On my server i keep a log of operations and this is tracked using the timestamp generated by MYSQL NOW() function.But the problem is that the timestamp generated is in UTC and i want the NOW() function to generate the timestamp corresponding to my local time for Asia/Kolkata. I tried the SET timezone but it is not working
$queryt="SET time_zone='Asia/Kolkata'";
mysqli_query($mysqli, $queryt))
What am i doing wrong?
Your question is not clear but if you want to change date and time value from UTC to your time zone, you can use
CONVERT_TZ(date, 'UTC', 'Asia/Kolkata')
You can select the UTC date and time value in your time zone:
SELECT CONVERT_TZ(date, 'UTC', 'Asia/Kolkata') AS local FROM table
If CONVERT_TZ function does not work, you need to install time zones (time zones should be already stored in MySQL). One note is that you need to store UTC value in your database and later convert to whatever time zone you want.
Try this:
SET GLOBAL time_zone = "Asia/Kolkata";
SET time_zone = "+05:30";
SET ##session.time_zone = "+05:30";
When you're using PHP (as the OP), if you can't install the CONVERT_TZ function, another option to convert a timezone is to use an offset interval.
In PHP:
date_default_timezone_set('Asia/Kolkata'); // Set timezone if different from default
$TZ_OFFSET = date('Z')/3600; // Timezone offset in hours from UTC (allows for DST where applicable)
Then run MySQL query using that offset variable:
SELECT (modified_date + interval $TZ_OFFSET hour) AS modified_date FROM table;
This is useful if you want to use "NOW()" on a database server set to UTC, but want to query date/times in a local timezone in an application.

How to convert now() into GMT +8 before insert into mysql

This is my sql to insert current time when a request password is made.
$stmt2 = $pdo->prepare('UPDATE authsessions SET reset_req=now(), reset_req_uuid=:reset_uuid WHERE useruuid=:user_id');
This causes a confusion because it follows the server's timezone to insert the time.
Therefore in my localhost, it inserts Asia/Kuala Lumpur time as I declared it in the page.But in the amazon server which I believe GMT it inserts accordingly. But I want to enforce it to insert time as in Asia/Kuala Lumpur.It's because the site meant to be used within Malaysia and there's no point using GMT time.
How do I make now() to convert to GMT 8+ before inserting, please?
You can convert the timezone in the statement using CONVERT_TZ (see MySQL manual).
First of all it depends on the MySQL field you are using. If you're using TIMESTAMP field it will be stored as UTC timestamp in the database. This is the most easy one, because your php connection will use the timezone of your local server. In this way you don't have to convert any timezone issues.
If you are using DATETIME field it will store the time as it is. Mysql NOW() will use the database timezone setting and you can override the setting when you connect with:
SET time_zone = '+08:00';
The other option you have is to handle the Timezone from PHP. Example:
$now = new DateTime('NOW', new DateTimeZone('Asia/Kuala_Lumpur'));
$stmt2 = $pdo->prepare('UPDATE authsessions SET reset_req=:now, reset_req_uuid=:reset_uuid WHERE useruuid=:user_id');
$stmt2->bindParam(':now', $now->format('Y-m-d H:i:s');

Timezone conflict between PHP and Mysql server

I have an issue with the date creation. The php server settings for timezone :
by using the date_default_timezone_get() php function, refers to UTC whereas the MSQL query for timezone i.e ##system_time_zone is referring to MST. It is a shared server, so I will not be able to make changes in the my.conf file on the server.
I want the date to be in UTC format, How do I go about making changes on the mysql server so that the Now() function and CURRENT_TIMESTAMP returns date values wrt UTC.
If I understood you right you can try and set a timezone per session from client code
SET time_zone = timezone;
Depending on whether you have timezone info imported or not it may look like
SET time_zone = 'Etc/UTC';
or
SET time_zone = '+00:00';
Further reading:
MySQL Server Time Zone Support
Try fetching date from mysql query using UTC_TIMESTAMP() instead of NOW(), UTC_TIMESTAMP() will give you date in UTC.

PHP use CURRENT_TIMESTAMP from eastern time zone

I have a query that looks like:
$query = "INSERT INTO `SmsServ`.`SmsReceived` (
`Id` ,
`Body` ,
`DateReceived`
)
VALUES ( NULL , "Foo" , CURRENT_TIMESTAMP);"
The problem with that query is that I will like to have eastern time as the time stamp. I have tried calling:
date_default_timezone_set("America/New_York");
At the begining of my php script but that still gives an incorrect time stamp.
You did change the php time zone. So try to change the mysql time zone aswell(php and mysql time default zones differs from each other):
mysql> SET GLOBAL time_zone = 'America/New_York';
mysql> SET SESSION time_zone = 'America/New_York';
CURRENT_TIMESTAMP in this case is not generated by PHP, but by MySQL. Your query is asking MySQL to set the current timestamp based on MySQL's server time. As such you would need to configure MySQL to use the eastern time zone, not PHP.
One thing you might consider is to just use GMT for database timestamps and do timezone and daylight savings conversions in the application. That way you don't potentially have the issue of mixed zone timestamps in the database. Of course, if you don't think you would ever have need to use anything other than Eastern time zone in your app, then this might not be important for you.
Mysql is not concerned about which timezone is using ..
As per documentation you can set the timezone to be used....if you are using MySQL 4.1.3 or +
SET GLOBAL time_zone = timezone
or
SET time_zone = timezone
NOTE : TIMEZONE settings are not populated by default. You need to populate the tables related to timezones under `mysql database . oNly after the settings you can set the timezone.

What timezone does MySQL TIMESTAMP columns use ON UPDATE?

Does it use the server timezone or UNIX_TIMESTAMP (UTC) ?
And how do I get the current time in PHP, in the same timezone as MySQL's TIMESTAMP columns?
Depends on your MySQL configuration variables, see MySQL Server Time Zone Support.

Categories