Web Hosting Talk







View Full Version : PHP Time Zone


ballingtonma
06-29-2004, 10:40 AM
Hi, using PHP, I want to show the date in the members time zone. Their time zone preference will be saved in a database. All I need to know is how to modify date(""); to acomidate for this, or if there is another simple way.

Thanks in advance,

Matt

Syphic
06-29-2004, 10:56 AM
have you tried any of the other types of time functions? I dont know if they would give you the time of that persons computer but its worth a shot hers the link...

http://us2.php.net/manual/en/ref.datetime.php

If that doesnt work maybe javascript could do it.

Syphic

ballingtonma
06-29-2004, 11:06 AM
I know there is a specific way to do it in PHP, I remember seeing it done before. I dont want any user side coding such as javascript.

Syphic
06-29-2004, 11:08 AM
I wouldnt either... well i gave you the url oddly enough its messed up ill change that... but hope you find it

Syphic

ballingtonma
06-29-2004, 11:42 AM
I dont think there what I'm looking for, thanks anyway!
Anyone else know how I can do it?

ballingtonma
06-30-2004, 06:04 AM
Come on guys please, someone must know, i really need this.
Thanks

Burhan
06-30-2004, 10:25 AM
Only works if your server is setup to use PHP as a CGI, not as a module.


$current_tz = getenv("TZ");
echo "Current Time (".$current_tz.") : ". date("h:i:s")."\n";
putenv("TZ=US/Eastern");
echo "New Time (Eastern): ". date("h:i:s")."\n";
putenv("TZ=".$current_tz);
echo "Current Time (".$current_tz.") : ". date("h:i:s")."\n";


There are other ways, but this should do it for you.

ballingtonma
06-30-2004, 11:39 AM
Thanks, I spent ages searching before you replied and found this:
<?php
$hourdiff = "8";
$timeadjust = ($hourdiff * 60 * 60);
$date = date("l, tS F Y",time() + $timeadjust);
echo "$date";
?>
It works well and how I want it too, but I was wondering if there a way to set it so the server time appears as GMT standard, ie London so therefore it appears in Londons time when I just use this date(""); and would like it to appear in GMT I think I would need putenv for this, but dont know how to do it or if it is possible.
I want this so I can put $hourdiff = "1"; which would return (GMT+01:00).

Also I was wondering if date() takes in to account daylight saying times or not, if not is there a way to get it to do so.
Thanks

Burhan
06-30-2004, 11:55 AM
The I format string for date() will tell you if time is in daylight savings or not.

You can get the current time zone by getenv().

ballingtonma
06-30-2004, 12:11 PM
Thanks,
this returns no timezone :(:
$current_tz = getenv("TZ");
echo "<br />Current Time ($current_tz) : ". date("h:i:s")."\n";

Also, how would I use putenv("TZ="); to set it as GMT?

Burhan
06-30-2004, 12:30 PM
A few reasons why it doesn't work :

1. Your host has it disabled
2. You are running php in ISAPI (module) mode

putenv("TZ=GMT") should do it.

ballingtonma
06-30-2004, 12:53 PM
Thanks thats working great!

RealSecurity
07-16-2004, 12:52 AM
Use gmdate() to generate your dates in GMT, then store them in the database as such. Then for each user, store their GMT offset (e.g. -5 for Eastern Time) and then use strtotime() on the date in the database, add the (offset *3600) and then use date() to format it.