[SOLVED] Time Zones

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

[SOLVED] Time Zones

Post 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
Last edited by Nay on Wed Dec 03, 2003 2:07 am, edited 1 time in total.
Chambrln
Forum Commoner
Posts: 43
Joined: Tue Dec 02, 2003 10:45 am
Location: Oregon

Post 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.
User avatar
mrvanjohnson
Forum Contributor
Posts: 137
Joined: Wed May 28, 2003 11:38 am
Location: San Diego, CA

Post 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

&lt;form action="" method="get"&gt;
  &lt;select name="location" id="location"&gt;
    &lt;option value="America/Los_Angeles" selected&gt;America/Los_Angeles&lt;/option&gt;
    &lt;option value="America/New_York"&gt;America/New_York&lt;/option&gt;
  &lt;/select&gt;
&lt;/form&gt;
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.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post 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
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

2mrvanjohnson
putenv("TZ=....") isn't thread safe ;(
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Ahhh yes...

My lack of research. Times like these are classics :lol:

Thanks again Mac,

-Nay
Post Reply