2017-03-02 82 views
-5

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

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

我想它显示26:30:00 not as 02:30:00

+4

请不要操纵太阳系... – NDFA

+0

DateTime对象代表一个时间点,而不是一段时间。 –

+0

@BehradKhodayar:是的同意:) !!!你怎么能得到大于24小时的格式。 –

回答

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; 
+0

@ Ehsan Ilahi .......请检查我的代码 –

+0

,但我已将您的代码与我的代码合并...只是为了向您展示我只定义了我的代码 –

+0

此代码现在正在工作......感谢您的帮助@Eshan IIahi –

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 

?> 
相关问题