PHP 转换snmp的时间格式

function convert_snmp_timeticks($timeticks) {
    if(!is_numeric($timeticks)) {
        return FALSE;
    }
    
    //如果是字符串类型,则先进行转换
    if(is_string($timeticks)) {
        $timeticks = intval($timeticks);
    }
    
    $TICKS_PER_SECOND = 100;
    $TICKS_PER_MINUTE = $TICKS_PER_SECOND * 60;
    $TICKS_PER_HOUR   = $TICKS_PER_MINUTE * 60;
    $TICKS_PER_DAY    = $TICKS_PER_HOUR * 24;
    
    $seconds = $timeticks / $TICKS_PER_SECOND % 60;
    $minutes = $timeticks / $TICKS_PER_MINUTE % 60;
    $hours   = $timeticks / $TICKS_PER_HOUR   % 24;
    $days    = $timeticks / $TICKS_PER_DAY;
    
    return sprintf("%d天, %d小时%02d分%02d秒", $days, $hours, $minutes, $seconds);
}

echo convert_snmp_timeticks("81090200");

输出:
    9天, 9小时15分02秒

 

 

更简单的方式,使用php官网的例子

if(@ $sysUpTime = snmpget("127.0.0.1", "public", "iso.3.6.1.2.1.1.3.0")){
    if(snmp_get_quick_print()){
       sscanf($sysUpTime, "%d:%d:%d:%d.%d",$day,$hour,$minute,$sec,$ticks);
        $sysUpTime = "$day days, $hour:$minute:$sec.$ticks";
    }else{
       $sysUpTime = ereg_replace("Timeticks: \([0-9]+\) ","",$sysUpTime);
   }
}
var_dump($sysUpTime);

输出:

    string(18) "9 days, 9:28:37.00"

 

posted @ 2015-12-26 11:02  Waichung  阅读(648)  评论(0编辑  收藏  举报