1

I am storing a date in UTC timezone inside my table but my need is to export a CSV file by converting a date from UTC to America/New_York time zone.

I have tried with CONVERT_TZ MySQL function, but it's giving null.

How I can convert a date from UTC to America/New_York time zone using only MySQL and not using PHP?

Eugene Lisitsky
  • 12,113
  • 5
  • 38
  • 59
Brajesh Kanungo
  • 129
  • 2
  • 13

2 Answers2

2

You have to load the timezone table.

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p password

If you do not have superuser access, you can skip friendly timezone names and specify the hours.

CONVERT_TZ(date,'+00:00','-07:00')
user3606329
  • 2,405
  • 1
  • 16
  • 28
1

The reason behind you are getting null result once you are using CONVERT_TZ MySQL function is that TZ time zone table have not been setup.

You can check that time zone table is set up or not. select * from mysql.time_zone; If it's giving null values then you need:

  1. insert time zone in MySql if you want to use CONVERT_TZ MySQL function to convert a date from UTC to America New_Yark time zone.

  2. You can run below query without update time_zone table.

SELECT DATE_SUB( order_date, INTERVAL 5 HOUR ) as OrderDate FROM TABLE_NAME

With date format:

SELECT DATE_FORMAT( DATE_SUB( order_date, INTERVAL 5 HOUR ) , '%Y-%m-%d %h.%i.%s %p' ) as OrderDate FROM TABLE_NAME

Please have a look similar question.

MySQL CONVERT_TZ()

It will help you in "How to insert timezone in MySql".

Community
  • 1
  • 1
Manraj
  • 205
  • 1
  • 3
  • 10