note 63233 added to function.strtotime
| From: | AlexanderVasarab at osu1 dot php dot net | Date: | Thu, 16 Mar 2006 16:48:41 +0000 |
| Subject: | note 63233 added to function.strtotime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-105938@lists.php.net to get a copy of this message | ||
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
?>
----
Server IP: 64.71.164.2
Probable Submitter: 69.171.56.90
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.strtotime.php
Edit -- http://master.php.net/note/edit/63233
Del: integrated -- http://master.php.net/note/delete/63233/integrated
Del: useless -- http://master.php.net/note/delete/63233/useless
Del: bad code -- http://master.php.net/note/delete/63233/bad+code
Del: spam -- http://master.php.net/note/delete/63233/spam
Del: non-english -- http://master.php.net/note/delete/63233/non-english
Del: in docs -- http://master.php.net/note/delete/63233/in+docs
Del: other reasons-- http://master.php.net/note/delete/63233
Reject -- http://master.php.net/note/reject/63233
Search -- http://master.php.net/manage/user-notes.php