PHPRO.ORG

Convert tai64 to timestamp

Convert tai64 to timestamp

Many applications including the ubiquitoius qmail, use tai64n time format for thier logs. A tai64 number looks rather cryptic to the unprepared but here we show a simple method of converting this to a human readable format. The function below returns a unix timestamp which we can use with PHP's date() function to create dates in any human readable format we wish. Here we also see the date() function used to create a MySQL type TIMESTAMP.


<?php

error_reporting
(E_ALL);

// an example tai64n number as supplied by qmail ***/
$tai64_number '@400000003c675d4000fb2ebc';

echo 
date('Y-m-d h:i:s'tai64_to_timestamp($tai64_number));

/**
*
* Convert a tai64 to a unix timestamp
*
* @usage
* $tai64_number = '@400000003c675d4000fbebc';
* echo tai64_to_timestamp($tai64_number);
*
* @string A valid tai64 number
*
* @return INT unix timestamp
*
*/
function tai64_to_timestamp($tai64_number){

    
/***  remove @ sign ***/
    
$tai64_number str_replace'@4''0'$tai64_number );

    
/*** strip last 8 chars ***/
    
$tai64_number substr$tai64_number0, -);

    
/*** convert to unix timestamp and hexdec ***/
    
return intvalhexdec$tai64_number ) );
}

?>