(I originally posted this in the main PHP manual some time in early 2002 and an
article outlining this amongst other things may soon circulate somewhere on u-
magazine.com or elsewhere.)
I used some help from the web as far as the javascript element goes, but the rest is
my own strange reasoning.
I learned everything the hard way when moving to a server that was out of our time
zone, and discovered that all of the posts in our message board were three hours
behind us. I also got in a bit of an argument with one of the main PHP.net guys
about why "simple" things like this should be more important than functions like
Output Buffering.
Anyhow,
<?
//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 the script above. It makes sense though, on the first visit
// to the page, it automatically redirects to the same page, with the time zone
// 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.
// 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):
// I'm not sure why there isn't 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 exclude client-side programming
// altogether. 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.
// I have been using this date/time setup for a couple of months now, and to be
// honest, I had forgotten we even used it. This is a good thing, because at first I
// assumed it would all be a bit too distracting or annoying, but I was far wrong.