Page 1 of 1
Local times messed up... I need DST detection
Posted: Wed Aug 24, 2011 1:14 pm
by condoravenue1
My site has lots of times posted. I want all of them to be correctly adjusted to where the user is, even if they are in daylight savings time. Here is some of my code:
Code: Select all
$user = mysql_query("SELECT * FROM perm WHERE username = '$username'");
while($row = mysql_fetch_array($user)) {$time_offset = $row['time_offset'];}
$hours = (int)$time_offset;
$minutes = ($time_offset * 60) % 60;
I then use the $hours and $minutes variables to adjust all the displayed times. Is there a way i can detect if a user is in a location that observes daylight savings time, then adjust $hours by + 1 or -1?
Re: Local times messed up... I need DST detection
Posted: Wed Aug 24, 2011 1:26 pm
by AbraCadaver
Don't store their offset, store their timezone. Then you can use that to set the timezone or use it in strtotime() or other method to get the user's time. This will calculate automatically whether there is daylight savings time or not.
Re: Local times messed up... I need DST detection
Posted: Wed Aug 24, 2011 1:45 pm
by condoravenue1
Right now a number such as "5.5" is stored. What format should it be in?
Re: Local times messed up... I need DST detection
Posted: Wed Aug 24, 2011 2:01 pm
by AbraCadaver
Probably the easiest would be like America/Chicago:
http://us2.php.net/manual/en/timezones.php
Unless someone has a better idea.
Re: Local times messed up... I need DST detection
Posted: Thu Aug 25, 2011 5:57 am
by condoravenue1
Code: Select all
date("F j, Y g:i a", strtotime($last_post[$i]))
I use the strtotime() function, but I don't know how to use 'America/Chicago' to adjust it.
Re: Local times messed up... I need DST detection
Posted: Thu Aug 25, 2011 7:07 am
by AbraCadaver
date_default_timezone_set()
Re: Local times messed up... I need DST detection
Posted: Thu Aug 25, 2011 7:31 am
by condoravenue1
Code: Select all
date_default_timezone_set('Americas/Chicago');
echo date("F j, Y g:i a", strtotime($last_post[$i]));
I put this at the top, and it has no effect. I tried several timezones.
Re: Local times messed up... I need DST detection
Posted: Thu Aug 25, 2011 8:19 am
by AbraCadaver
Code: Select all
error_reporting(E_ALL);
ini_set('display_errors', '1');
It's not America
s.
Re: Local times messed up... I need DST detection
Posted: Thu Aug 25, 2011 9:28 am
by condoravenue1
date_default_timezone_set('America/Chicago');
also doesn't work
Re: Local times messed up... I need DST detection
Posted: Thu Aug 25, 2011 9:51 am
by AbraCadaver
condoravenue1 wrote:date_default_timezone_set('America/Chicago');
also doesn't work
What does it display?
Re: Local times messed up... I need DST detection
Posted: Thu Aug 25, 2011 11:12 am
by condoravenue1
I ran this code:
Code: Select all
<?php
echo date("F j, Y g:i a", strtotime('2011-08-23 15:04:52')) . "<br><br>";
date_default_timezone_set('Asia/Kolkata');
echo date("F j, Y g:i a", strtotime('2011-08-23 15:04:52')) . "<br><br>";
date_default_timezone_set('America/Chicago');
echo date("F j, Y g:i a", strtotime('2011-08-23 15:04:52')) . "<br><br>";
?>
And got this result:
August 23, 2011 3:04 pm
August 23, 2011 3:04 pm
August 23, 2011 3:04 pm