Rasher's Toolbox

Back

findtimediff

mixed findtimediff([string server [, int port [, int timeout]]]) -- Find the number of seconds your local clock is wrong

Description

This function contacts a daytime (RFC-867) server and finds how much your local clock is off and returns the number of seconds. If an error occurs, the function returns false. If no parameters are passed, the default is to contact a NIST time server with a timeout of 10 seconds.

Example

<?php
  
echo "Your local time is ".findtimediff()." seconds off";
?>

This will output:

Your local time is -277 seconds off

Source

Published under the terms of the BSD License

<?php
function findtimediff($server "time-a.nist.gov"$port 13$timeout 10) {
    
$sp = @fsockopen($server$port$errno$errstr$timeout);
    if (!
$sp) {
        return 
false;
    }
    else {
        list(
$mjd$date$time$tt$l$h$msADV$utcnist$otm) = explode(" "trim(fread($sp256)));
        
$remote strtotime($date." ".$time) + date('Z');
        
fclose($sp);
    } 
    
$local time();
    return 
$remote $local;
}
?>

Last updated: Sat Jun 14 20:02:45 CEST 2008

Valid XHTML 1.0! Valid HTML 3.2!