8

I have a database storing records in UTC timestamp. I want to fetch them in terms of local time standard(IST).

By referring to some referral i tried like this.

select date(convert_tz(sa.created_at,'+00:00','+05:30')) as date,count(*) as hits from session_acts sa, sessions s where sa.session_id = s.id and s.created_at between convert_tz('2015-03-12T11:33:00+00:00','+00:00','-05:30') and convert_tz('2015-03-13T11:33:00+00:00','+00:00','-05:30') group by date;

But it will resulting in

+------------+------+
| date       | hits |
+------------+------+
| 2015-03-12 |   94 |
| 2015-03-13 |   34 |
+------------+------+

I want to display only hits that are requested on 13th. Where i am going wrong.?

3 Answers 3

17

IST is 5.30 hours ahead of UTC, so when 13th starts in IST i.e. 2015-03-13 : 00:00:00 its 2015-03-12 18:30:00 in UTC

mysql> select convert_tz('2015-03-13T00:00:00+00:00','+00:00','+05:30') ;
+-----------------------------------------------------------+
| convert_tz('2015-03-13T00:00:00+00:00','+00:00','+05:30') |
+-----------------------------------------------------------+
| 2015-03-12 18:30:00                                       |
+-----------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)

And when 13 ends in IST i.e. 2015-03-13 : 23:59:59 its 2015-03-13 18:29:59 in UTC

mysql> select convert_tz('2015-03-13T23:59:59+00:00','+00:00','+05:30') ;
+-----------------------------------------------------------+
| convert_tz('2015-03-13T23:59:59+00:00','+00:00','+05:30') |
+-----------------------------------------------------------+
| 2015-03-13 18:29:59                                       |
+-----------------------------------------------------------+

So yo get the data in IST for 13th you will need to search data within this range of dates.

So the condition would be as below -

s.created_at 
between convert_tz('2015-03-13T00:00:00+00:00','+00:00','+05:30')
and convert_tz('2015-03-13T23:59:59+00:00','+00:00','+05:30');

and since you are doing conversion at the time of select so it will return all 13th data.

3
  • 1
    i used s.created_at between convert_tz('2015-03-13T00:00:00+00:00','+00:00','-05:30') and convert_tz('2015-03-14T00:00+00:00','+00:00','-05:30');
    – Aparichith
    Mar 20, 2015 at 11:37
  • @h.APP.y good catch you can actually use convert_tz('2015-03-14T00:00+00:00','+00:00','-05:30') which you have done. Mar 20, 2015 at 11:46
  • Thank you so much. I stuck with this issue last few days. You Saved my day. Nov 28, 2018 at 11:06
1
select convert_tz('2015-03-13T00:00:00+00:00','+00:00','+05:30') ;
1
  • Please add some explanation to your answer such that others can lean from it
    – Nico Haase
    May 28, 2020 at 6:26
0

MySQL Query - As per Indian Time zone

select CONVERT_TZ(CURRENT_TIMESTAMP,'+00:00','+12:30');

Output:

CONVERT_TZ(CURRENT_TIMESTAMP,'+00:00','+12:30')
2022-06-15 17:36:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.