note 18966 added to function.date
| From: | office at at universalmetropolis dot com | Date: | Tue, 12 Feb 2002 00:24:01 +0000 |
| Subject: | note 18966 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-26491@lists.php.net to get a copy of this message | ||
To make up for my "stupidity" in the confusion as what local time was, and smart-aleck
editor remarks, and that it didn't occur to me until we switched to a server out of our time
zone, here is what I discovered today:
I used some help from the web as far as the javascript element goes, but the rest is my own strange
reasoning.
--
// first lets introduce the main page displaying the local-to-user date and time. As I was working
on forums scripts, lets call this forums.php
// the way our forums are constructed is that all the programming is done, then the layout is
compiled and parsed in one giant template, which is very fast.
// this is part of the programming before the page template is parsed
// if the user time zone cookie hasn't been set (on the first visit to a page, it won't
have been), open the javascript include that will appear in the page's template
if(!$usertimezone){
// special amendment for date/time conversion
// open javascript template/include
$checktimezone=parsetemplate("checktimezone");
// this javascript looks like this:
// <script type="text/javascript">
now = new Date();
window.location.href="$PHP_SELF?tz=" + now.getTimezoneOffset();
</script>
// somebody else created this script. It makes sense though, on the first visit to the page, it
automatically redirects to the same page, with the timezone variable ("tz") passed through
the URL.
}elseif($tz){
// the next time a page in the forums has been accessed, and the cookie has not yet been set, set a
cookie with the user's time zone (from GMT) AND create a variable for the time zone, because
data in a cookie won't be seen until the next header request, and you want everything to take
effect NOW.
$usertimezone=$tz;
setcookie("usertimezone",$usertimezone);
}
// next page is your functions page
// user date/time with local time zone
function userdate($format,$timestring){
// declare globals (that being the cookie and variable you have set)
global $usertimezone;
// convert user time zone into seconds
$timezone=$usertimezone*60;
// find timezone difference between server and UST in seconds
$difference=date("Z");
// difference between user and server
$usertime=$timestring-$difference-$timezone;
// return new value
return(date($format,$usertime));
}
// end user date/time
// then, when you want to display the date and time local to the user, you call it like this, just
like you would a normal "date()" function, but allowing for the new function you have just
created (assuming you used a time() string for your date):
$posttime = userdate($your_date_format,$time_of_post);
--
This is really unnecessary and I can't believe I wasted a day on it.
May I suggest that PHP spend less time on fancy-pants functions like Output Buffering, and more time
ensuring the basics are taken care of? Remember, not everybody in PHP came from C++ backgrounds, and
one cannot assume that what is obvious to one, is obvious to another.
Isn't there some way to read the time/date headers in a page request from the user's
browser like you do with everything else in the language, compare that to the server's time,
and voila?
If you can figure out a user's IP address, browser type and credit card number, you can
certainly figure out the difference between their time and the server's.
--
http://www.php.net/manual/en/function.date.php
http://master.php.net/manage/user-notes.php?action=edit+18966
http://master.php.net/manage/user-notes.php?action=delete+18966
http://master.php.net/manage/user-notes.php?action=reject+18966