dongwei1954 2015-09-04 13:17
浏览 80
已采纳

以天,小时,分钟和秒显示刻度

I'm trying to show an UpTime in Days, Hours, Minutes, Seconds. Something like 20 days, 4 hours, 9 minutes, 3 seconds

Here is my PHP Code:

// Get uptime with my SNMP class
$iTicks = $oHardwareMonitoring->fGetSystemUpTime();

// Convert Ticks to seconds
$iSecondes = $iTicks / 100;

// Convert seconds to Days, Hours, Minutes, Seconds
$sSecondes = gmdate('s', $iSecondes);
$sMinutes  = ($sSecondes > 60 ? round(($sSecondes / 60), 0) : null);
$sHeures   = ($sMinutes > 60 ? round(($sMinutes / 60), 0) : null);
$sJours    = ($sHeures > 24 ? round(($sHeures / 24), 0) : null);

// Show the result
echo '<b>'.$sInfosUptime.'</b> : '.
($sJours != null ? $sJours.' '.DAY.' ' : null).
($sHeures != null ? $sHeures.' '.HOUR.' ' : null).
($sMinutes != null ? $sMinutes.' '.MINUTE.' ' : null).
$sSecondes.' '.SECONDE;

When I execute the PHP, I get 38 Seconde(s) for 429859 ticks.

How to show the uptime correctly?

  • 写回答

2条回答 默认 最新

  • doulian4467 2015-09-04 13:59
    关注

    $sSecondes can never be greater than 60 because you use gmdate('s', $iSecondes); which returns a value between 00 and 59. Therefore the conditions that follow will never be evaluated as true.

    Using the following line:

    $sMinutes = ($iSecondes > 60 ? round(($iSecondes / 60), 0) : null);
    

    returns:

    1 HOUR 72 MINUTE 38 SECONDE

    Better but not exactly what is expected.

    We can get the proper amount of each unit by using modulo, division and floor():

    $sSecondes = $iSecondes%60;
    $sMinutes  = floor($iSecondes%3600/60);
    $sHeures   = floor($iSecondes%86400/3600);
    $sJours    = floor($iSecondes/86400);
    

    Which returns:

    1 HOUR 11 MINUTE 38 SECONDE

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么