Blog

Down and Dirty Timezone Migration

Our application at work is very dependent on users having their timezone set. We store all our event times in UTC/GMT and thus we need the user’s timezone to properly display it for them so it makes sense. Turns out we missed some spots where we weren’t setting timezone plus we had a fair amount of early accounts that never had it set cuz timezone came later.

So most all of our accounts have an address so i figured we could look up the timezone based on the address. I first looked at Google’s geocode api and we could use that to submit and get the timezone based on address but they only allow 25k requests per day for free. I had to convert some ~424K so i could do a few every day for several days of maybe not.

Next I thought well i already know the address and based on state you can figure out the basic timezone. I know time is hard in programing cuz of all the strange geography across the us, but i figured i could 90% covered by just doing the basics.

So i just used the following array to compare the state and then set the timezone based on the match. I still need to updated it for Canada provinces but this is working fine for now.

$tz_states = array(
'America/Los_Angeles' => array('CA', 'NV', 'OR', 'WA'),
    'America/New_York' => array('CT', 'DE', 'FL', 'GA', 'ME', 'MD', 'MA', 'NH', 'NJ', 'NY', 'NC', 'OH', 'PA', 'RI', 'SC', 'VT', 'VA', 'DC', 'WV'),
    'America/Anchorage'=>array('AK'),
    'Pacific/Honolulu'=>array('HI'),
    'America/Boise'=>array('ID'),
    'America/Chicago'=>array('AL', 'AR', 'IL', 'IA', 'KS', 'LA', 'MN', 'MS', 'MO', 'NE', 'OK', 'SD', 'TN', 'TX', 'WI'),
    'America/Denver'=>array('CO', 'MT', 'NM', 'UT', 'WY'),
    'America/Detroit'=>array('MI'),
    'America/Indiana/Indianapolis'=>array('IN'),
    'America/Kentucky/Louisville'=>array('KY'),
    'America/North_Dakota/Center'=>array('ND'),
    'America/Phoenix'=>array('AZ'),
);

full code can be found here