Let’s say I have a PHP script that does this:
$timeNow = 1349852400000; // Unix time for Oct 10, 2012 00:00:00 PDT
echo "<script type='text/javascript'>";
echo "setTimeout(function(){alert('time out!');}, $timeNow - Date.now());";
echo "</script>";
What if this script is served to a device whose date/time are set to 2013. What will happen? What if this is served to a device whose date/time are set to 2010?
Does it matter what time and date the server’s clock is set to? How does that affect the device in 2013? What about the one stuck in 2010?
If I wanted to choose a value for $timeNow that will throw the alert at the moment everywhere synced on October 10, 2012 at 00:00:00 for San Francisco, CA (that is, a device that’s set to Pacific time and a device set to Central time should display the alert at the same moment even though it will be 2:00:00 AM for the Central time device), could I do it? Won’t I have to serve one value to the Pacific device and a different one for the Central device to account for the time zone difference from UTC?
Sorry, time zones are really confusing for me. I’m having trouble wrapping my head around them.
You don’t have anything to worry about if you use Unix time for this (with one exception below). It sounds like you want the alert to pop up all over the world at the exact same instant no matter the timezone.
Unix time 0 is
So if you arranged for an alert to go off at time 0, people in San Francisco and London would see the alert at the same time. Ditto for any Unix time, which is really just a number that describes an instant in time. The same instant everywhere in the world. So both of the following datetime strings
are representations of the same instant, denoted by Unix time 0.
Similarly everyone in the world sees the time 1349852400000 at the exact same instant, regardless of their time zone. So since your code is trying to alert something at a given instant described by a Unix time, your code in principle should work, provided the computers on which the script is running are set to the correct time.
I suppose if you really want to have a bunch of computers all alert something at the same time (are you writing a virus? Just kidding) and you want to handle the case where the devices may have improperly set clocks, you can have them make calls to a central time server and set your timeout based on that. Then you will probably have to deal with cross-site issues, but in principle, if all the machines agree to talk to and accept data from a central time server, you can do this.
ADDENDUM
Here is how that might work. Let’s say your script is running on a bunch of machines. They all have their clocks all set wrong. Maybe they set their clocks wrong on purpose; maybe they set their clocks wrong accidentally. But you want them all to alert at the same time.
So what your script should do is this: make an (Ajax) call to your server — the same server that delivered the HTML page in which the script is embedded, to simplify cross-site issues. This call talks to your server, which knows the real time. Now you also know the time from now until the alert time. So substract those two values on your server and deliver back to the clients that number. Then the clients do
and you are good to go.