|
|
(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. |
|
| Validator 98 - a PHP-script to generate form-validation-code in JavaScript. Categories : Complete Programs, Java Script, PHP, HTML and PHP | | | Dynamic Calendar in PHP, Javascript and HTML. Categories : PHP, Java Script, HTML and PHP, Calendar | | | 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 | | | Convert date's in YYYY-MM-DD (i.e. mysql format) into PHP3 timestamps. Also Find the difference in days between two PHP3 timestamps. Categories : HTML and PHP, PHP, MySQL, Date Time | | | This PHP function creates dropdown select lists for time and date that you can change, outputs a 14 char MySQL timestamp in a text field Categories : PHP, MySQL, Java Script, HTML and PHP | | | Kewl Date Example Categories : PHP, HTML and PHP, Date Time, CSS, Beginner Guides | | | Newbie Notes #9 - Hyperlinking a post Categories : PHP, Java Script, HTML and PHP, Beginner Guides | | | enhanced date picker with jcript checking for a dynamic date input Categories : PHP, Java Script, Date Time, Calendar, Arrays | | | 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 | | | Function that allows a Javascript cookie to be set after HTML has been outputted to the page.
Categories : PHP, Java Script, Cookies, HTML and PHP | | | Calendars to choose a range of dates , reservation events ... Categories : PHP, Calendar, Java Script, Date Time | | | Customizable Calendar Class Categories : HTML and PHP, Date Time, PHP, PHP Classes, Calendar | | | PHP3: Formmail. Just a cgi formmail, but than in PHP. It is easy to use! Categories : HTML and PHP, Email, PHP, Perl, HTML and PHP | | | 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 | | | PHP Calendar Categories : PHP, Calendar, Date Time, Java Script, CSS | |
| |
| |
|