2012-05-19 63 views
2

例如,如果我有:有没有内置到PHP的转换秒到几天,几小时,分钟?

$seconds = 3744000; // i want to output: 43 days, 8 hours, 0 minutes 

我必须创建一个函数来转换呢?还是PHP已经有内置的东西来做到这一点,如date()

+1

您可以轻松地用做['日期()'函数(HTTP: //php.net/manual/en/function.date.php) – Lix

+0

我试过日期(),但它不会计算超过31天。我做错了什么? – supercoolville

+1

@supercoolville你没有做错任何事,'date()'不会做你想做的事。 –

回答

13
function secondsToWords($seconds) 
{ 
    $ret = ""; 

    /*** get the days ***/ 
    $days = intval(intval($seconds)/(3600*24)); 
    if($days> 0) 
    { 
     $ret .= "$days days "; 
    } 

    /*** get the hours ***/ 
    $hours = (intval($seconds)/3600) % 24; 
    if($hours > 0) 
    { 
     $ret .= "$hours hours "; 
    } 

    /*** get the minutes ***/ 
    $minutes = (intval($seconds)/60) % 60; 
    if($minutes > 0) 
    { 
     $ret .= "$minutes minutes "; 
    } 

    /*** get the seconds ***/ 
    $seconds = intval($seconds) % 60; 
    if ($seconds > 0) { 
     $ret .= "$seconds seconds"; 
    } 

    return $ret; 
} 

print secondsToWords(3744000); 
+0

接近http:// stackoverflow。 com/a/4798608/138383 –

+0

从一些旧的代码中得到它 - 但它最初可能来自Google上的同一个源代码。 –

+0

“bcmod”(它将字符串作为参数并在编译PHP时需要'--enable-bcmath')而不是标准模运算符'%'的任何原因? (http://uk.php.net/manual/en/language.operators.arithmetic.php) – IBBoard

2

这是非常简单和容易找到PHP核心天,小时,分钟和秒:

$dbDate = strtotime("".$yourdbtime.""); 
$endDate = time(); 
$diff = $endDate - $dbDate; 
$days = floor($diff/86400); 
$hours = floor(($diff-$days*86400)/(60 * 60)); 
$min = floor(($diff-($days*86400+$hours*3600))/60); 
$second = $diff - ($days*86400+$hours*3600+$min*60); 

if($days > 0) echo $days." Days ago"; 
elseif($hours > 0) echo $hours." Hours ago"; 
elseif($min > 0) echo $min." Minutes ago"; 
else echo "Just now"; 
相关问题