If you have your date components split up already the way you do, you
should be using mktime().

Next you have to determine which timezone your information is from.  If
you consider your month/day/year values to be GMT values, then use
gmmktime() to generate your timestamp.  Otherwise, make sure you
putenv("TZ=whatever") the timezone that matches your data before your call
to mktime().

It is important to understand that a UNIX timestamp (seconds since 1970
epoch) carries no timezone information.  A single UNIX timestamp
represents one specific second all over the world.  That is, if I gave you
the timestamp for when my baby was born, it would be the exact second this
happened.  This might be Monday here, but Tuesday in Australia.  Timezone
information is only applied once you go to display a human-readable date
from the unix timestamp.  gmdate() is just a special-case that does about
the same as:

   $old = getenv("TZ");
   putenv("TZ=GMT");
   echo date("M d Y H:i:s",$timestamp);
   putenv("TZ=$old");

It is in there because GMT is needed for all sorts of stuff and we figured
having a shortcut version of the above was a good idea.

If you really want to use strtotime() you can just tack the timezone
associated with your data onto the end of the string.  In your example:

  strtotime("{$_POST['month']}/{$_POST['day']}/{$_POST['day']} GMT");

if your month/day/year are in GMT.

-Rasmus

On Thu, 29 May 2003, Justin French wrote:

> I have $_POST['day'] (1-31), $_POST['month'] (1-12), $_POST['year']
> (2003-2004).
>
> I store all dates as unix (epoch) timestamps, and generate the timestamp for
> the event with:
>
> strtotime('{$_POST['month']}/{$_POST['day']}/{$_POST['day']}');
>
> Everything works fine on my LAN (Australia), but when I upload my database
> to the live server (US), the dates are a day behind (when echo'd with
> date('Y-m-d')), presumably due to the time difference between the two
> servers.
>
> Timestamps generated directly on the live server appear correct when echoed
> out using date.
>
>
> How can I reliably generate timestamps which will work on any server, and
> reliably convert these timestamps to human readable format (eg
> date('Y-m-d')) on any server?
>
>
> Is the key to use GMT in the strtotime() string and use gmdate() instead of
> date()?  Or is it more complex than that?
>
>
> Thanks,
>
> Justin French
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to