Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MySQL – Convert string with timezone into Date

I would like to convert strings such as "Tue, 15 May 2012 17:26:44 EST" into UTC dates, so that I can then convert them into UNIX timestamps.

I tried the following but can’t see a parameter for timezones in the MySQL documentation

SELECT STR_TO_DATE("Tue, 15 May 2012 17:26:44 EST", "%a, %d-%b-%Y %T");

I think I can also use COVERT_TZ() but the records could be any timezone so I’m not sure how to to determine the parameters.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Using CONVERT_TZ you can get the last word from your string (which hopefully is always the time zone) and convert it to UTC:

SELECT 
  CONVERT_TZ(
    /*date:*/
    STR_TO_DATE('Tue, 15 May 2012 17:26:44 EST', '%a, %d %b %Y %T'),
    /*from_tz: select the last word, EST in this case:*/
    SUBSTRING_INDEX(TRIM('Tue, 15 May 2012 17:26:44 EST'), ' ', -1),
    /*to_tz:*/
    'UTC') 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading