RE: [PHP] local time v.s. server time
| From: | Joe Sheble \(Wizaerd\) | Date: | Sat, 25 Aug 2001 15:45:31 +0000 |
| Subject: | RE: [PHP] local time v.s. server time | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-64437@lists.php.net to get a copy of this message | ||
Thanx James for your answers... perhaps I'm missing a piece of the puzzle,
but here is how I corrected or fixed it.
In my included file that gets included in every app and every page, I
placed:
putenv( "TZ=America/Phoenix" );
and all my dates are now local time. SO saving to a mySQL database now I
merely pass in:
$now = date( "Y-m-d H:i:s" );
$result = mysql_query( "INSERT INTO table ( datetime ) values( '$now' )",
$dbConn );
If I ever distribute this app (doubtful) I'll include a fairly extensive
listing of timezones so the putenv() could be appropriately changed... the
list I found and would most likely include is here:
http://www.theprojects.org/dev/zone.txt
> -----Original Message-----
> From: James, Yz [mailto:liljim@btconnect.com]
> Sent: Saturday, August 25, 2001 8:11 AM
> To: php-general@lists.php.net
> Subject: Re: [PHP] local time v.s. server time
>
>
> Hi Joe,
>
> GMT isn't affected by daylight savings (altough in the UK we are using
> British Summer Time at the moment, which is GMT +1), so that's
> the constant
> you can work from if you're not experiencing daylight savings
> schemes where
> you are. I have written out an example for you below.
>
> We get the Y-m-d H:i:s format of time using gmdate() (which Mysql likes).
> We convert that lot to a timestamp. The function I've created takes two
> arguments: whether to add or take time from the timestamp that is
> generated,
> and the offset in hours. So, to emulate what Mysql's now() function does,
> you can do something like this
>
> <?
>
> $now = GenerateOffset("-", "6"); // takes off 6 hours.
> $query = @mysql_query("INSERT INTO table (datetime) VALUES ('$now')",
> $connection);
>
> ?>
>
> And you should get correct datetime formats for your timezone into your
> table (providing you have put your correct offset from GMT).
>
>
> <?
>
> function GenerateOffset($plusminus, $offset) {
>
> $datetime = gmdate("Y-m-d H:i:s");
> list($date, $time) = explode(" ", $datetime);
> list($year, $month, $day) = explode("-", $date);
> list($hour, $minute, $second) = explode(":", $time);
>
> $timestamp = mktime($hour, $minute, $second, $month, $year, $day);
>
> if ($plusminus == "+") {
> $newtime = $timestamp + (3600 * $offset);
> } else {
> $newtime = $timestamp - (3600 * $offset);
> }
>
> $newdate = date("Y-m-d H:i:s", $newtime);
>
> return $newdate;
> }
>
> ?>
>
> Hope this helps.
>
> James
>
> "Joe Sheble )" <JustForMailingLists@wizaerd.com> wrote in message
> > Alas, I don't have this luxury since it's not my server, but one that is
> > hosted with a web hosting provider. They control the setup and
> > configuration of the machines... since it's a shared server I doubt I'll
> > convince them to set the timezone to my location, thus throwing everyone
> > else on the same server out of whack...
> >
> > I can use the putenv() function though for use in PHP and then
> when saving
> > the date and time into mySQL actually use the PHP date and time
> functions
> > instead of the mySQL Now() function...
> >
> > Where would I find the TZ codes to use for my area?
> >
> > thanx
> >
> > > -----Original Message-----
> > > From: Don Read [mailto:dread@texas.net]
> > > Sent: Friday, August 24, 2001 8:39 PM
> > > To: Joe Sheble (Wizaerd)
> > > Cc: General PHP List
> > > Subject: RE: [PHP] local time v.s. server time
> > >
> > >
> > >
> > > On 25-Aug-2001 Joe Sheble \(Wizaerd\) wrote:
> > > > My website is hosted with a provider, and there is a three hour
> > > difference
> > > > in timezones, so when saving date and times to the database,
> > > they reflect
> > > > the server time and not my own local time. The clincher is I
> > > know I could
> > > > do some time math and just substract 3 hours, but I live in
> > > Arizona, so we
> > > > do not go through daylight savings time. So right now it's a three
> hour
> > > > difference, but when the time change happens, I'll only be two
> > > hours behind.
> > > >
> > > > Because of this, what is the best method to get my local
> date and time
> > > > entered into the database instead of the server date and time??
> > > >
> > >
> > > In my case the server is in Atlanta, but I have to sync with my
> > > credit-card
> > > processor on the left coast.
> > >
> > > So first i start the database (MySQL) on Pacific time with:
> > > -----
> > > TZ=PST8PDT
> > > export TZ
> > > /usr/local/bin/safe_mysqld --user=mysql > /dev/null &
> > >
> > > ----
> > > To make PHP date/time functions jive, during initialization:
> > >
> > >
> > > putenv('TZ=PST8PDT'); // Server on Pacific time
> > >
> > > No matter that i'm in Texas (CST), everything is now reported on Pac
> time.
> > >
> > > Regards,
> > > --
> > > Don Read dread@texas.net
> > > -- It's always darkest before the dawn. So if you are going to
> > > steal the neighbor's newspaper, that's the time to do it.
> > >
> > >
> >
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>
>
>