Assume someone in my database was born at -73440000
unix time. This means he's born on 04-Sep-1967 UTC but 03-Sep-1967 EDT. How would I count the number of people born on each day of the year in EDT?
Right off the bat, you will discover that
SELECT FROM_UNIXTIME(-73440000)
-- returns NULL
Returns NULL
. MySQL can't handle negative unix timestamps.
Fine, we can work around that:
select date_add('1970-01-01', interval -73440000 second)
-- returns 1967-09-04 00:00:00
Yields 1967-09-04 00:00:00
which is his birthdate in UTC.
We can try to convert this to EDT (Toronto time):
select convert_tz('1967-09-04 00:00:00','UTC','America/Toronto')
-- returns 1967-09-04 00:00:00, but should be 1967-09-03 20:00:00
But as it turns out, CONVERT_TZ
doesn't work on dates prior to 1970 either.
(I've installed the timezones already and it does work on dates between 1970 and 2038)
So now I'm stuck. I need to convert the unix timestamp to a MySQL DATE so that I can GROUP BY it and then COUNT it. The only other option I can think of is to return every record in the database as unix timestamps and use another language to do the conversion and tally them up, but that could get a bit ridiculous if there's millions of records.
N.B. You can't calculate the hour offset between EDT and UTC either because that can change throughout the year (daylight savings).