How to make MySQL() NOW() function use client's timezone? - mysql

I am using the NOW() function to store date and time, but the server timezone ( which is my own ) will obviously not work for the whole world.
What I want is for now() always to use the CLIENT timezone, whichever it may be.
How can this be achieved?

NOW() executes on current execution timezone (DB Server Timezone). You can use CONVERT_TZ(dt,from_tz,to_tz) function along with NOW() to get a different timezone representation like
SELECT CONVERT_TZ(NOW(),'GMT','EST');
Either way, unless you determine the TZ info of client; at DB end, your DB server has no way determining that.

Related

How to configure Code Workbook's timezone for CURRENT_TIMESTAMP?

How can I configure Code Workbooks's timezone such that calling CURRENT_TIMESTAMP in SQL returns my local time instead of the default UTC?
Are there any reasons this would be advised against?
Timestamps in Spark have no concept of timezones since they represent the number of microseconds since Unix epoch. I'm not aware of any Code Workbook setting to change this, but Spark likely uses the system clock of whatever host it executes the function on to determine what the current timestamp should be, and it wouldn't be possible to fiddle with those settings.
Sounds like what you're looking for is some function like from_utc_timestamp, which takes your timestamp in UTC and shifts it to your timezone. Note that your timestamp would still be timezone-agnostic, but if you were to print the string representation of your timestamp, it would now look like the wall-clock date/time in your local timezone.

Mysql: Set time_zone for select queries, UTC for everything else

Is it possible to configure MySQL (at runtime) to use UTC for all write queries, but another timezone for all SELECT queries?
I'm storing all dates in UTC (additionally, the app will always pass UTC datetimes to MySQL) and have MySQL configured to be running in UTC (using SET time_zone = '+0:00'); but is there a way for it to automatically translate all SELECT into another timezone?
I realise I can do this in code, but if I can make MySQL do the job for me that'd be much easier.
To be clear: I'd also like to just run a command once, not use any of the date/time formatting functions for each query.
User CONVERT_TZ() in mysql function
SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET');
Ref : http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_convert-tz

Changing CURRENT_TIMESTAMP value based on timezone in MySQL

I want to migrate our mysql server from shared hosting to local server.
Current server is in MST time zone and the values for the CURRENT_TIMESTAMP in databsse is stored as -7:00 GMT.
Now I want to move complete application on dedicated server in India. Also want to convert the date values stored in -7:00 GMT to +5:30 GMT.
I can accomplish this task of updating the dates by writing script to update the time, however I would like to know if is there any way I can do this from database itself (at time of import or while exporting itself)
mysql version 5.0.96-log. I am not getting option export timestamp in UTC.
When using mysqldump, set the flag: --tz-utc to force all timestamps to be exported as UTC. ( http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_tz-utc ). Note here --tz-utc is enabled by default. So you should have to do nothing: but test first :)
If just working with timestamps on the server you don't have to do anything to convert them, from the documentation on TIMESTAMP post MySQL 4.1 ( http://dev.mysql.com/doc/refman/4.1/en/timestamp.html ):
"values still are stored in UTC, but are converted from the current
time zone for storage, and converted back to the current time zone for
retrieval. As long as the time zone setting remains constant, you
get back the same value you store. 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 is easy to test:
Save a timestamp to your table
Change the server's timezone
Retrieve it: the return value should reflect the new timezone.
So another option is you could just have both the servers set to the same timezone while doing the export / import, than set them back to the correct timezone(s) after it is complete, but note with MySQLDump this should not be necessary.
General syntax
SELECT DATE_ADD(<column_name>, INTERVAL HOUR);
for changing to UTC to MST
SELECT DATE_ADD(NOW(), INTERVAL 7 HOUR);

Get unix timestamp from mysql database with offset applied

I have the offset in seconds to UTC, all timestamps stored on the database are in UTC, how to apply the offset during selection queries,
mysql_query("SELECT * FROM table WHERE unix_timestamp=unix_timestamp with offset applied");
mysql_query("INSERT INTO table (unix_timestamp) VALUES(UNIX_TIMESTAMP())");
I'd want to insert in UTC time to the database and retrieve in local time.
On PHP:
date_default_timezone_set('UTC');
$user_timezone_offset=-7200; // -2
Could I substract on select's runtime of mysql to the unix_timestamp field $user_timezone_offset and that would be it?
I am inserting in UTC which is what I need apparently, but for selection in user's local time I'd need to apply the offset to the timestamp stored in the database somehow.
Edit: I just came across this:
$user_timezone_offset="-2:00";
FROM_UNIXTIME(CONVERT_TZ(unix_timestamp,'+00:00','$user_timezone_offset'))
Would need to know how to apply it to a select query making the selection of unix_timestamp converted for comparison inside the select and the result also becoming modified for php's fetch_array
This seems to be the solution:
SELECT *, Unix_Timestamp(CONVERT_TZ(FROM_UNIXTIME(unix_timestamp), '+00:00', '$user_timezone_offset')) as unix_timestamp
Actually this has a problem - although on
mysql_fetch_array($result){ $row['unix_timestamp']; }
comes converted while checking on
WHERE unix_timestamp=value //assuming unix_timestamp
to be converted on the select - in there it is not converted and another
Unix_Timestamp(CONVERT_TZ(FROM_UNIXTIME(unix_timestamp), '+00:00', '$user_timezone_offset'))
has to be called for unix_timestamp to be "converted" each time a check is to be made against the converted value.
I think you may be overcomplicating the issue.
Timestamps are always UTC. When you retrieve a timestamp in PHP, it is in UTC.
When you convert a timezone using the date() function for example, it displays in whatever timezone the php.ini setting date.timezone is set to.
Therefore, if you have users with specific timezones, just call date_default_timezone_set() with the appropriate timezone prior to displaying dates in local time.
If you use the DateTime class, then you can specify the desired timezone when you construct the object.
Unless you have a very specific reason, there should be no need to perform timezone conversions when you fetch timestamps from the database. PHP handles this all for you.

How do I change the mysql server date using SQL

I need to change the server date via sql.
Preferably through my application (CL is also good).
How do I change the mysql server date using SQL?
Why:
We test a scenario that spans several weeks. To test it we need to advance the server date by that time.
Do you want to change the server date or the server timezone?
If it is the server date, you have to do it at the OS level.
If it is the time zone, you can change it globally for entire database instance or for your session/connection only.
The SET TIMESTAMP statement affects the value returned by NOW() but not by SYSDATE()