note 63233 deleted from function.strtotime by derick

From: Date: Sun, 08 Apr 2007 13:04:43 +0000
Subject: note 63233 deleted from function.strtotime by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-125216@lists.php.net to get a copy of this message
Note Submitter: Alexander Vasarab ---- I'm not sure whether I've overcomplicated this example, but here is my shot at the perfect timezone class. Upon construction, it takes the users timezone (perhaps a setting pulled from a database or session) and the timezone in which the server is located, and the MySQL datetime to be localized. So, for example, if you make a news post to your website at 11:30AM in EST (-5), and a user which is located on the west coast of the US (PST -8) views your website the post time will be displayed in THEIR time (8:30AM). The date() format can be customized as a second parameter of the convert function. Works with any standard timezone. Hopefully, this will save somebody time in the future. <?php class timezone { var $user_offset; // users timezone location var $default_offset = -6; // default server timezone location, in this case, US Central Time (-6) var $difference; var $datetime; function timezone($user_offset, $default_offset = NULL) { $this->user_offset = $user_offset; if(isset($default_offset)) $this->default_offset = $default_offset; $this->difference = ($this->user_offset - $this->default_offset); // determine the difference between the users timezone and the servers } function convert($datetime, $format = 'M j, Y \a\t g:iA') { $this->difference = (($this->difference >= 0) ? '+' : '') . $this->difference; // add a + in front of the difference if positive if(eregi('.5', $this->difference)) $difference = substr_replace($this->difference, '', -2, 2) . ' hours +30 minutes'; // account for a timezone with .5 in it else $difference = $this->difference . ' hours'; $this->datetime = strtotime($datetime); $timestamp = strtotime($difference, $this->datetime); return date($format, $timestamp); } } /* All possible timezones that will work with this class: array(-12, -11, -10, -9, -8, -7, -6, -5, -4, -3.5, -3, -2, -1, 0, 1, 2, 3, 3.5, 4, 4.5, 5, 5.5, 6, 7, 8, 9, 9.5, 10, 11, 12); */ $users_timezone = 1; // could be stored in a database or $_SESSION $date = '2006-03-16 06:16:33'; // a date/time in US Central Time (CT -6) $timezone = new timezone($users_timezone); // create an instance of class timezone print $timezone->convert($date); // returns "Mar 16, 2006 at 1:16PM" to somebody in Paris ?>

« previous php.notes (#125216) next »