I have a mySQL table with a date recorded from various different Time Zones. How can I convert the DateTime value to the viewing user TimeZone while doing a SELECT statement in mySQL ?
SELECT ClientID,
convert_tz(MessageDate,???,???)
MeetingType,
MeetingDate,
Comments,
CompanyName
FROM clientCompNotes
WHERE ClID = 970392281 AND CompanyID=411972145
The problem is to accept any timezone from the date recorded in the "MessageDate" field, which looks like this
Wed May 30 2012 12:51:02 GMT-0400 (Eastern Daylight Time)
Many thanks
Dennis
You shouldn't need to do this. MySQL will perform timezone conversion for you, provided that you store your times in TIMESTAMP
type columns and that you set the session time_zone
variable appropriately. As stated in the manual:
The MySQL server maintains several time zone settings:
[...]
Per-connection time zones. Each client that connects has its own time zone setting, given by the session
time_zone
variable. Initially, the session variable takes its value from the globaltime_zone
variable, but the client can change its own time zone with this statement:mysql> SET time_zone = timezone;The current session time zone setting affects display and storage of time values that are zone-sensitive. This includes the values displayed by functions such as
NOW()
orCURTIME()
, and values stored in and retrieved fromTIMESTAMP
columns. Values forTIMESTAMP
columns are converted from the current time zone to UTC for storage, and from UTC to the current time zone for retrieval.The current time zone setting does not affect values displayed by functions such as
UTC_TIMESTAMP()
or values inDATE
,TIME
, orDATETIME
columns. Nor are values in those data types stored in UTC; the time zone applies for them only when converting fromTIMESTAMP
values. If you want locale-specific arithmetic forDATE
,TIME
, orDATETIME
values, convert them to UTC, perform the arithmetic, and then convert back.