What timezone does MySQL TIMESTAMP columns use ON UPDATE? - php

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.

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.

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.

Can PHP safely use unix timestamps to query mysql database?

There's lots of recommendations out there about handling dates. I'd just like to clarify something. Let's say:
user is inserting records into a database
unix timestamps of insertion date are generated for this record
Now the user wants to query a date interval in the database:
user provides 2 dates in his local timezone
use these values to convert timezone to UTC and get the timestamp
query the records in the database based on the 2 integers from conversion (eg. WHERE date >= FIRST and date <= SECOND)
convert the retrieved timestamps to local timezone again for display
I know that would be possible with PHP, but and wouldn't need to care about mysql's timezone settings in this case - only php's. The system would be 64 bit so running out of space to store the date is not an issue. But ...
Would that raise any other serious issues like with DST changes or something else?
Unix timestamp is timezone-independent.
This is also the reason you can change this step:
use these values to convert timezone to UTC and get the timestamp
into this:
convert values to Unix timestamp
Although storing timestamps in the database (eg. MySQL) is very simple. You can make sure PHP has Unix timestamp, if you will:
save the values by using FROM_UNIXTIME() MySQL's function (give Unix timestamp as argument and you will receive datetime according to MySQL's settings),
retrieve the values by using UNIX_TIMESTAMP() MySQL's function (give the name of the field, or the value, as the argument), so you will get Unix timestamp (integer) on the basis of datetime stored in the database according to MySQL's settings.
Just remember to use TIMESTAMP column type to store timestamps. This way the time will be stored in timezone-independent manner, only displayed according to MySQL's settings.
Tadeck is correct that Unix timestamps are timezone-independent.
But when using timestamps throughout your application you should store and use timestamps in the database as plain INTs. Convert to and from local timezones at the application level (in PHP). That allows you to only concern yourself with timezones in PHP and not in 2 systems. It also eases setting time zones for individual users at the application level.

current timestamp = tomorrow

I have recently changed my server time zone to America/New_York.
The problem is when I run my php/mysql script to add rows into the database with default: CURRENT_TIMESTAMP, it adds the date as tomorrow instead of today.
Then when tomorrow rolls over at midnight on my server, the dates go back to yesterday?
MySQL uses its own time zone settings and PHP uses its own time zone settings.
Make sure when you configure your PHP time zone settings, set the same time zone settings for MySQL as well.
Oh and by the way, Timestamp data type in MySQL doesn't store time zone information, If you supply timezone to MySQL when writing to this field, MySQL will convert it to UTC based on its timezone settings.
SELECT ##global.time_zone, ##session.time_zone; -- perform this in mysql console
If it gives incorrect result - change the mysql server timezone explicitly in config file:
http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html

Categories