|
|
|
(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.
$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);
// 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. |
|
| Select with current month Categories : PHP, HTML and PHP, Date Time, Arrays | | | Dynamic Calender in PHP, Javascript and HTML. Categories : PHP, Java Script, HTML and PHP, Calendar | | | Pull Down Surfing - Surf on Change Categories : Java Script, MySQL, HTML and PHP, PHP, Databases | | | Creates three SELECT form fields: Month, Day, and Year. You give it a string which will be used to make the name for the three fields, and a number of seconds to use as the default date. If you give it blank for this value, the current date is used. Categories : HTML and PHP, PHP, Date Time | | | If you want to create select buttons featuring current date this example will show you how... Categories : Date Time, HTML and PHP, PHP | | | Remote Scripting: send form POST data to a script and insert the results into a page without refreshing the page. Categories : PHP, AJAX, HTML and PHP, Java Script | | | CALENDAR - easy calendar-navigation with PHP Categories : PHP, Date Time, HTML and PHP, Calendar | | | This script contains 2 functions: 1 to create html select object based on your own customer date format entry- "M d Y h:i.... etc". The second function processes the select object on submit back to unix time. Categories : PHP, Calendar, Date Time, HTML and PHP | | | Website colour changer Categories : PHP, HTML and PHP, Date Time | | | Dynamic generation of textboxes, select items etc in a table for use with databases applications, matrimonials and for job sites Categories : PHP, HTML and PHP, Java Script | | | Kewl Date Example Categories : PHP, HTML and PHP, Date Time, CSS, Beginner Guides | | | Calendars to choose a range of dates , reservation events ... Categories : PHP, Calendar, Java Script, Date Time | | | Tree Menu Dynamic (+Static) with Loading in Progress.. Categories : PHP, Java Script, HTML and PHP | | | OverEasy - PHP generated JavaScript to do mouseovers on
your pages. Modify one file and one function does it all
for you! Categories : PHP, Java Script, HTML and PHP, MySQL | | | Local Time clock and Server time usign PHP and JavaScript Categories : PHP, Java Script, Date Time, Beginner Guides | |
|
|
|