note 79037 added to function.localtime
| From: | hutch120 at gmail dot com | Date: | Thu, 08 Nov 2007 01:23:16 +0000 |
| Subject: | note 79037 added to function.localtime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-132092@lists.php.net to get a copy of this message | ||
The functions shown below may help implement a timezones library.
<?php
/**
* This function returns the server timezone offset in seconds
* e.g. Sydney in DST returns +1100 / 100 * 60 * 60 = 39600
*/
function getServerTimeZoneOffset()
{
return date("O") / 100 * 60 * 60; // Seconds from GMT
}
/**
* This function returns the local timezone offset in seconds
* where getTimeZone($userid) returns a timezone see below.
*/
function getLocalTimeZoneOffset($userid)
{
return getTimeZone($userid) / 100 * 60 * 60; // Seconds from user GMT
}
// Now converting a server timestamp to a local timestamp is very simple.
/**
* Will take a timestamp and minus off Server GMT and add on user GMT seconds
* thereby making a local timestamp from a server timestamp.
*/
function getLocalTimestampFromServerTimestamp($userid, $timestamp)
{
return $timestamp - getServerTimeZoneOffset() + getLocalTimeZoneOffset( $userid );
}
//And getting the local time from a user entered date is a useful function.
//Combine the next two functions to achieve just that.
function getLocalTimestampFromDateTime($userid, $datetime)
{
$timestamp = strtotime($datetime);
return getLocalTimestampFromServerTimestamp($userid, $timestamp);
}
/**
* If you have a localized timestamp and just want to get the date format use this.
*/
function getFormattedDate($userid, $timestamp)
{
return date("Y-m-d H:i:s " . getTimeZone($userid), $timestamp);
}
/**
* Returns a timezone in the format +0000
* e.g. Perth in DST returns +0900
*/
function getTimeZone($userid)
{
return "+0900"; // Perth in DST
}
?>
----
Server IP: 203.16.234.20
Probable Submitter: 124.171.62.57
----
Manual Page -- http://www.php.net/manual/en/function.localtime.php
Edit -- https://master.php.net/note/edit/79037
Del: integrated -- https://master.php.net/note/delete/79037/integrated
Del: useless -- https://master.php.net/note/delete/79037/useless
Del: bad code -- https://master.php.net/note/delete/79037/bad+code
Del: spam -- https://master.php.net/note/delete/79037/spam
Del: non-english -- https://master.php.net/note/delete/79037/non-english
Del: in docs -- https://master.php.net/note/delete/79037/in+docs
Del: other reasons-- https://master.php.net/note/delete/79037
Reject -- https://master.php.net/note/reject/79037
Search -- https://master.php.net/manage/user-notes.php