Hi!

Here is the script that shows the time of the client system. I added quite a bit of commentary to it, so it should be obvious what it does.
I wrapped it into a function, but that is not really necessary.

As mentioned earlier the core piece is the JavaScript, which I did not create, but some smart person on the ECLUG mailing list. The rest may be trivial to you, but I think it is darn smart and I'm proud of it. :)

Have fun and show your clients that you are ticking right.

David

<?php
function showclienttime() {
    // -----------------------------------------------------------------------
    // Define variables
    // -----------------------------------------------------------------------
    $fct_severtimedata = array();
    $fct_servertime = "";
    $fct_serverbias = 0;
    $fct_clientbias = 0;
    $fct_totalbias = 0;
    $fct_clienttimestamp = 0;
    $fct_year = "";
    $fct_month = "";
    $fct_day = "";
    $fct_hour = "";
    $fct_minute = "";
    $fct_second = "";
    // -----------------------------------------------------------------------

    // Make nice time string following international standards
    // Get client GMT bias
    // JavaScript creates a cookie if it does not exists and writes the client
    // GMT bias in it, then unfortunately reloads the page to read out the
    // cookie value to have it available for PHP to pick up
    // JavaScript contributed from a smart person of ECLUG
    if(!isset($_COOKIE['GMT_bias'])) {
    ?>
    <script type="text/javascript">
    var Cookies = {};
    /***
    * @name = string, name of cookie
    * @value = string, value of cookie
    * @days = int, number of days before cookie expires
    ***/
    Cookies.create = function (name, value, days) {
      if (days) {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    var expires = "; expires=" + date.toGMTString();
    } else {
    var expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
    this[name] = value;
    }
    var now = new Date();
    Cookies.create("GMT_bias",now.getTimezoneOffset(),7);
    window.location = "<?php echo $_SERVER['PHP_SELF'];?>";
    </script>
    <?php
    } else {
    $fct_clientbias = $_COOKIE['GMT_bias'];
    }

    // Get server GMT bias
    $fct_servertimedata = gettimeofday();
    $fct_servertime = $fct_servertimedata['sec'];
    $fct_serverbias = $fct_servertimedata['minuteswest'];

    // Assume that the client is in the western european time zone (e.g. 
Germany).
    // That gives currently a bias for Eastern US of 240 and for Germany one of 
-120.
    // In this case one can subtract the -120 from the 240, which gives 
240-(-120) = 360.
    // This is correct as Germany is six hours ahead.
    // Now the assumed client is in California and as such the GMT bias is 420.
    // Calculating again gives 240 - (420) = -180      
    // This means that when the result is negative one has to subtract the 
result from
    // server time, otherwise one needs to add it. Math comes to help as that
    // server time + (360) = server time + 360
    // and
    // server time + (-180) = server time - 180
    // Finally 1 minutes has 60 seconds so the client/server bias has to be 
multiplied
    // by 60 and then added to the unix style time stamp for the current server 
time 

    // Make total bias in minutes
    $fct_totalbias = $fct_serverbias - $fct_clientbias;
    // Transition total bias to seconds
    $fct_totalbias = $fct_totalbias * 60;

    // Make client timestamp
    $fct_clienttimestamp = $fct_servertime + $fct_totalbias;

    // Split time/date and format nicely
    $fct_time = time();
    $fct_year = strftime("%Y", $fct_clienttimestamp);
    $fct_month = strftime("%m", $fct_clienttimestamp);
    $fct_day = strftime("%d", $fct_clienttimestamp);
    $fct_hour = strftime("%H", $fct_clienttimestamp);
    $fct_minute = strftime("%M", $fct_clienttimestamp);
    $fct_second = strftime("%S", $fct_clienttimestamp);

    // Pad minutes and seconds with leading zero
    $fct_minute = str_pad($fct_minute, 2, "0", STR_PAD_LEFT);
    $fct_second = str_pad($fct_second, 2, "0", STR_PAD_LEFT);

    // Print date and time
    echo $fct_year."-".$fct_month."-".$fct_day." 
".$fct_hour.":".$fct_minute.":".$fct_second;

    // End of function
}  
?>
_______________________________________________
New York PHP Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk

NYPHPCon 2006 Presentations Online
http://www.nyphpcon.com

Show Your Participation in New York PHP
http://www.nyphp.org/show_participation.php

Reply via email to