Page 1 of 1
[SOLVED] Time Zones
Posted: Tue Dec 02, 2003 11:53 am
by Nay
I'm developing my CMS and got stuck when letting the user adjust to his/her time zone. I'd thought of adding or subtrating and came up with:
Code: Select all
<?php
$str = "12:58, 11-09-2003";
$arr = explode(":", $str);
$str = $arr[0];
$str = $str + 23;
if($str > 24): $str = $str - 24; EndIf;
$str = $str . ":" . $arr[1];
echo $str;
?>
Well, I'm also letting the user customize the format of the date, so 12:58, 11-09-2003 won't always be static. Problems would start then. The time is drawn from a mysql timestamp.
So is there anyway around this method, since then the user would have to be stuck with a 24 hour time which imho, many don't like. I tried googling for this kind of a situation didn't have much luck <_<.
Any suggestions?
Thanks,
-Nay
Posted: Tue Dec 02, 2003 12:02 pm
by Chambrln
The way I would probably do it is use the php time() stamp and subtract the amount of seconds you need then display it as a 12 hour time.
ex.
Code: Select all
<?php
$time = mktime(12,58,00,11,9,2003);
//To get number of seconds needed to find desired time you first need to know how many hours away they are. You should probably store this number in the database along with their use information or how ever you are storing info about that person.
$hours = ($hour*60*60); //60 minutes per hour, 60 seconds per minute
//Next subtract or add the seconds to the overall time
$time += $hours;
echo date("g:i a m/d/Y", $time);
?>
This is a very rough idea. I don't know if you can add a negative number and still get the same result as subtracting a positive with PHP. I haven't had to do that. If so you're done. If not, you need to determine if they are less hours away or more hours away and subtract or add accordingly.
Posted: Tue Dec 02, 2003 12:07 pm
by mrvanjohnson
For setting the time I would create an interface would allow the user to define a variable you could then use with the [php_man]putenv[/php_man].
For example
Code: Select all
<form action="" method="get">
<select name="location" id="location">
<option value="America/Los_Angeles" selected>America/Los_Angeles</option>
<option value="America/New_York">America/New_York</option>
</select>
</form>
then on your process page something like
Code: Select all
<?php
$local = $_GET ['location'];
putenv("TZ=$local");
?>
That is just one way. Using math might be the better way to go if there server is not on GMT. I'm not sure how this would behave is the server was set to some local time.
As far formatting the date, check out your Profile on this board and notice how phpBB does it. There is a section in your profile where you define the format using the standard PHP date () syntax. Just do that, and again drop it into a variable for that user. You would obviously need to store all these settings in a database somewhere.
Posted: Wed Dec 03, 2003 1:26 am
by Nay
Chambrln,
You idea works quite good, I tested this out:
Code: Select all
<?php
$hours = -10;
$time = 20;
$time += $hours;
echo $time;
?>
I got 10, as expected. Anyhow, my only challenge now is converting a MySQL timestamp to a PHP timestamp so I can do a date().
-Nay
Posted: Wed Dec 03, 2003 2:07 am
by Nay
Yeah, it works now:
Code: Select all
<?php
$hours = +2;
$mins = 41;
$secs = ($hours * 60 * 60) + ($mins * 60);
$con = mysql_connect("localhost", "root",) or die(mysql_error());
$db = mysql_select_db("tests", $con) or die(mysql_error());
$sql = "SELECT DATE_FORMAT(date, '%H,%i,%s,%m,%d,%Y') AS fdate FROM news ORDER BY id DESC";
$result = mysql_query($sql, $con) or die(mysql_error());
$row = mysql_fetch_array($result);
$timestamp = $row['fdate'];
$d = explode(",", $timestamp);
$unix = mktime($d[0],$d[1],$d[2],$d[3],$d[4],$d[5]);
$stamp = $unix + $secs;
$final = date("H:i:s, d-m-y", $stamp);
echo $final;
?>
-Nay
Posted: Wed Dec 03, 2003 3:15 am
by Weirdan
2mrvanjohnson
putenv("TZ=....") isn't thread safe ;(
Posted: Wed Dec 03, 2003 3:19 am
by Nay
Adds setting to the server environment. The environment variable will only exist for the duration of the current request. At the end of the request the environment is restored to its original state.
So I wondered, what's different with using sessions? O_o
-Nay
Posted: Wed Dec 03, 2003 7:19 am
by twigletmac
You can get a UNIX timestamp from MySQL by using the UNIX_TIMESTAMP() function (it'll save you a bit of coding).
Mac
Posted: Wed Dec 03, 2003 7:28 am
by Nay
Ahhh yes...
My lack of research. Times like these are classics
Thanks again Mac,
-Nay