2017-04-18 58 views
2

好日子,MySQL的 - 將局部日期UTC格式

我有以這種格式存儲的mysql日期:

enter image description here

在我的SQL查詢我想在UTC格式,這些日期。 我試過CONVERT_TZ(dt,from_tz,to_tz)函數,但我無法確定如何從日期獲取from_tz。我知道,轉到to_tz將是「UTC」或「+0000」

+0

你可以參考這個https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_convert-tz –

回答

1

試試這個:

select convert_tz(`date`, replace(substring_index(`date`, ' ', -1), '00', ':00'), '+00:00') 

編輯:

select convert_tz(`date`, concat(left(substring_index(`date`, ' ', -1), 3), ':', right(substring_index(`date`, ' ', -1), 2)), '+00:00') 
+0

它一直在工作,直到我找到像這樣的時間戳:2017-04-17 09:21:33 +0530 ...在這種情況下,它給予NULL –

+0

@WarisAli檢查編輯部分。 – Blank

+0

是!那會的。謝謝! –

0

那麼,作爲MySQL的忽略日期TZ預選賽,我建議你做一個semimanual TZ轉換

select   
    date_add("2017-04-18 15:15:15 +1000", interval substring("2017-04-18 15:15:15 +1000", -5, 3) hour), 
    date_add("2017-04-18 15:15:15 +1000", interval substring("2017-04-18 15:15:15 -1000", -5, 3) hour) 
; 

結果將在UTC時區如您所願

+0

這是一個好方法,但它總是增加時間,即使當地時區是+0600,在這種情況下,它應該實際上減去時間。 –