PHPRO.ORG

Atomic Time

Atomic Time

This function will return the timestamp of the current atomic as set by the atomic clock at time-a.netst.gov. The returned unix timestamp value can be used with the date function to format the date in any human readable format.


<?php

/**
 *
 * @Get atomic time
 *
 * @throws Exception of failure
 *
 * @return int
 *
 */
function atomicTime()
{
    
/*** connect to the atomic clock ***/
    
$fp = @fsockopen"time-a.nist.gov"37$errno$errstr10 );
    if ( !
$fp )
    {
        throw new 
Exception"$errno$errstr);
    }
    else
    { 
        
fputs($fp"\n"); 
        
$time_info fread($fp49);
        
fclose($fp);
    }
    
/*** create the timestamp ***/
    
$atomic_time = (abs(hexdec('7fffffff') - hexdec(bin2hex($time_info)) - hexdec('7fffffff')) - 2208988800); 
    echo 
$errstr;
    return 
$atomic_time;
}

?> 

Example Code


<?php
    
try
    {
        
/*** get the timestamp ***/
        
$atomic_time atomicTime();

        
/*** format the time ***/
        
echo date('Y M d H:i:s'$atomic_time);
    }
    catch(
Exception $e)
    {
        echo 
$e->getMessage();
    }
?>

Demonstraton

2018 Dec 14 21:22:03