Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tlug] Getting a client and server to agree on UTC time



TLUG,

I'm writing a Javascript  application that talks to a PHP/MySQL server,
and I need them to conform to using UTC time so that users across time
zones can see information about other user's time without having to know
what time zone each other is in.

I display to the user everything time related in their local time, but
that's only when displaying. Behind the scenes, everything is converted
to UTC.

I use a Unix time stamp, stored as a ten digit number in MySQL (not as a
DATE field). I do all the time calculating within the PHP and Javascript
code. Accuracy to seconds is good enough, so on the Javascript end, I
convert from miliseconds to seconds.

Here's the code I use to get the UTC time on the Javascript side:
function convertLocalToUTC(timeInput)
{
    return timeInput + (new Date().getTimezoneOffset() * 60);
}

The variable "timeinput" is a ten digit Unix time stamp. Theoretically,
the Date().getImtezoneOffset() returns the number of minutes difference
between now and UTC. Multiplying it by 60 turns it into seconds, and can
be added to the number of seconds in the Unix time stamp.

Here's the code I use to get the UTC time on the PHP side.
$utcTime = time() - date("Z");

When I test on my local machine, everything works. But when I upload the
PHP to a remote server (hosted in Boston), the times begin to disagree.
They're off by a few hours.

I think that my math is correct, so long as I'm working on the right
assumptions about what UTC time values I should be getting back from
date("Z") and getTimezoneOffset. However, since things aren't working as
expected, my understanding of what those return must not be right.

Of course, I've looked up on online guides and the PHP manual, but what
I've read is what lead me to believe these should work in the first place.

Can anyone point out where I'm going wrong?

Any advice would be much appreciated.

--
Dave M G


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links