tags:

views:

123

answers:

3

I am working on a cron job which sends report to hundreds of users around the world. The cron should send an email to all my users at exactly 12am in the morning according to their timezone. Thanks in advance

A: 

Figure out what timezone you are in:

time("O");

Figure out what the time is where you are:

time("H:i")

Do some magic to work out whether you should be adding or subtracting time to get to midnight, then add or subtract the difference between your time and midnight to get the timezone difference, and add it to your own timezone. Voila.

Edit: See @TheTXI's answer for the 'magic' involved

Matthew Scharley
+2  A: 

You need a list of which timezone all your users are in + whether they're using daylight savings or not.

For a complete list of timezones see this wikipedia article

Then every 15 minutes you need to have cron run a script to all the users in the current timezone offset.

e.g. at UTC + 5:45 send your mails to all users in the "Kathmandu" timezone

Glen
+1 for looking out for users in Kathmandu!
Dominic Rodger
not just Kathmandu, Tehran is +3:30, Adelaide and Darwin in australia are +9:30, Delhi and Mumbai are +5:30. Basically timezones are usually in increments of 15 minutes. Also, basically, timezone handling sucks!
Glen
Yup, I seem to remember some country or other's leader randomly deciding to change its timezone last year by diktat - not sure how seriously that change got taken by people who care about timezones! In other words, I don't know where it says that timezones have to be on 15 minute boundaries.
Dominic Rodger
Yes Glen timezone handling really sucks. Thanks for making me feel better.
drikoda
@Dominic It's not set in stone that they have to be 15 minute boundaries, that's just what all the current ones use. Could be different tomorrow as it's basically a political decision
Glen
Glen: This may be stretching the boundaries of what the OP is asking for, and it may be cheating slightly, but you could in theory lump some of those others that fall in the 15/30/45 increments into other timezones so that they get sent out when the nice round ones do. Wouldn't be on the button midnight, but I think it wouldn't hurt to be flexible since the entire idea of sending the stuff around midnight is so it's delivered when people aren't around to see it immediately.
TheTXI
@TheTXI You could indeed do that. It's just not what the OP asked about. If their requirements allow it, then I'd say go for it. I'm all for breaking the rules as long as people understand (a) what the rules are and (b) the possible consequences
Glen
Glen: Correct. I would imagine that in the real world, the requirement that everything gets delivered to their mailbox at exactly midnight could be flexible enough to allow for the "incrementals" to fall under one of the rounded off hours. But if not, you can always break down and do the 15 minute increments like you suggested (which I have no problem with at all).
TheTXI
@TheTXI Thinking about it, e-mail doesn't guarantee that a mail gets delivered instantly anyway. Just because you send it at midnight doesn't mean it'll get delivered at midnight, it could be hours before it shows up in your inbox, so you're probably right, worrying about 15 minutes doesn't really make a lot of sense.
Glen
@Glen - especially if those pesky internet tubes get clogged up
Dominic Rodger
+1  A: 
Your Time | Timezone You Need
-----------------------------
12:00am     0
1:00am      -1
2:00am      -2
3:00am      -3
4:00am      -4
5:00am      -5
...
12:00pm     +/- 12
1:00pm      +11
2:00pm      +10
3:00pm      +9
...
------------------------------

You should be able to see the pattern start to develop here. The pattern should end up being something like Timezone Difference = (12-your current hour)

TheTXI
Don't forget all the places that use 15, 30 and 45 minute offsets as well
Glen
Thanks, you helped to think in another perspective. I think instead of the math work which is a lot confusing. i'll just hard code if statements for every timezone for every hour of my timezone.
drikoda
Good point Glen. And drikoda, I wasn't really advocating for having to go in and hard code everything in, but given a large and thorough enough list or database table (including breaking it up into the 15 minute increments instead of the one hour like I have done), but if that ends up being the best way for you to do it without wanting to kill yourself, then so be it.
TheTXI