|
Posted by ZeldorBlat on 08/10/06 16:09
Dennis de Wit wrote:
> Shai Halevi wrote:
> > I'm trying to write a general-purpose PHP code for the following task:
> >
> > Step 1: The user specifies some date in the future (call it "the
> > deadline"), which I parse via strtotime. This date is stored in the
> > database.
> >
> > Step 2: Every time the user returns to the site, it sees a message
> > saying, "deadline is XXX, time now is YYY". (Or maybe "deadline is XXX,
> > time left is YYY").
> >
> > So far no problem. But I also want the user to specify in Step 1 in
> > what time-zone these XXX and YYY should be displayed. For example, the
> > user can ask to specify the dates in America/New_York time, or in UTC,
> > or in the server's local time, or whatever.
> >
> > Moreover, this is to be a "general-purpose code", I don't know the
> > local time-zone of the server, nor can I make any assumptions ahead of
> > time about whether or not daylight saving time it observed in either
> > the server's time-zone or the target time-zone. (And if it is observed
> > in both, I don't know if the switch occurs at the same time in both.)
> > Finally, this should work with PHP 4.x (so no date_default_timezone_set
> > for me), and I cannot rely on the timezone environment variable being
> > available either.
> >
> > One possible way to solve this is to compute the offset between the
> > target time and the server time (or between the target time and UTC) at
> > the deadline. Can anyone explain to me how to do that under the
> > constraints from above?
> >
> > Thanks,
> >
> > -- Shai Halevi
> >
>
>
> First thing that came up in my mind is: calculate the time difference
> between your time and the time on the server. That would give you
> information on where the server is placed and what time it uses. You can
> ask the user to enter his timezone information and extract/add this to
> the time he enters so the time noted in the database is server-local-time.
>
> But.... I'm not really sure how you can tell wether a certain region is
> in daylight savings time. I know that my country (the netherlands) has
> different dates for daylight savings time compared to for example The
> United States. Also, I'm not really sure why you would take the daylight
> savings times into account.
>
> I hope this helps you out a bit.
>
> Dennis
The beauty is you don't need to worry about any of this. It took me
awhile to understand exactly how this works, but here's what happens:
strtotime() turns some representation of the time into a UNIX
timestamp. This timestamp is the number of seconds since the UNIX
epoch -- which is /always/ in UTC. In other words, depending on the
timezone setting of the server, strtotime() will return different
results for the same string. Furthermore, there is no daylight savings
in UTC, and strtotime knows how to handle that (again, based on the
timezone setting of the server). date() behaves exactly the same way
(just in reverse).
So, all you need to do is call date_default_timezone_set() before
calling strtotime() to get the correct UTC timestamp, then call
date_default_timezone_set() again before calling date() to make sure
it's formatted in the correct timezone.
Easy, eh?
Navigation:
[Reply to this message]
|