PHP DateTime():显示大于24小时的时间长度

时间:2017-03-02 04:48:28

标签: php

PHP DateTime():显示大于24小时的时间长度。

$time = new DateTime();
$time->setTime(26, 30);
echo $time->format('H:m:i');

我希望它显示26:30:00 not as 02:30:00

2 个答案:

答案 0 :(得分:0)

请尝试此代码

<?php
// convert time into seconds
function hoursToSecods($hour) { // $hour must be a string type: "HH:mm:ss"

    $parse = array();
    if (!preg_match ('#^(?<hours>[\d]{2}):(?<mins>[\d]{2}):(?<secs>[\d]{2})$#',$hour,$parse)) {
         // Throw error, exception, etc
         throw new RuntimeException ("Hour Format not valid");
    }

         return (int) $parse['hours'] * 3600 + (int) $parse['mins'] * 60 + (int) $parse['secs'];

}

// convert seconds into time
function time_from_seconds($seconds) { 
    $h = floor($seconds / 3600); 
    $m = floor(($seconds % 3600) / 60); 
    $s = $seconds - ($h * 3600) - ($m * 60); 
    return sprintf('%02d:%02d:%02d', $h, $m, $s); 
} 


$timeinsecond = hoursToSecods("26:30:00"); 
echo time_from_seconds($timeinsecond); 

// output  26:30:00

?>

答案 1 :(得分:0)

此代码现在正在运行:

  function convert_seconds($seconds) 
 {
  $dt1 = new DateTime("@0");
  $dt2 = new DateTime("@$seconds");
  return $dt1->diff($dt2)->format('%a days, %h hrs, %i min and %s sec');
  }


$user=& JFactory::getuser();
$totaltime=0;

foreach ($this->data[TOTALVISITEDPAGESPERUSER] as $user):
$totaltime += $user[7];     
endforeach;

        $getres=convert_seconds($totaltime * $this->cparams->get('daemonrefresh'));?>

echo $getres;