Local times messed up... I need DST detection

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
condoravenue1
Forum Commoner
Posts: 30
Joined: Fri Dec 03, 2010 10:24 pm

Local times messed up... I need DST detection

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Local times messed up... I need DST detection

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
condoravenue1
Forum Commoner
Posts: 30
Joined: Fri Dec 03, 2010 10:24 pm

Re: Local times messed up... I need DST detection

Post by condoravenue1 »

Right now a number such as "5.5" is stored. What format should it be in?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Local times messed up... I need DST detection

Post by AbraCadaver »

Probably the easiest would be like America/Chicago: http://us2.php.net/manual/en/timezones.php

Unless someone has a better idea.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
condoravenue1
Forum Commoner
Posts: 30
Joined: Fri Dec 03, 2010 10:24 pm

Re: Local times messed up... I need DST detection

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Local times messed up... I need DST detection

Post by AbraCadaver »

date_default_timezone_set()
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
condoravenue1
Forum Commoner
Posts: 30
Joined: Fri Dec 03, 2010 10:24 pm

Re: Local times messed up... I need DST detection

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Local times messed up... I need DST detection

Post by AbraCadaver »

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', '1');
It's not Americas.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
condoravenue1
Forum Commoner
Posts: 30
Joined: Fri Dec 03, 2010 10:24 pm

Re: Local times messed up... I need DST detection

Post by condoravenue1 »

date_default_timezone_set('America/Chicago');
also doesn't work
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Local times messed up... I need DST detection

Post by AbraCadaver »

condoravenue1 wrote:date_default_timezone_set('America/Chicago');
also doesn't work
What does it display?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
condoravenue1
Forum Commoner
Posts: 30
Joined: Fri Dec 03, 2010 10:24 pm

Re: Local times messed up... I need DST detection

Post 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
Post Reply