例如,如果我有:有沒有內置到PHP的轉換秒到幾天,幾小時,分鐘?
$seconds = 3744000; // i want to output: 43 days, 8 hours, 0 minutes
我必須創建一個函數來轉換呢?還是PHP已經有內置的東西來做到這一點,如date()
?
例如,如果我有:有沒有內置到PHP的轉換秒到幾天,幾小時,分鐘?
$seconds = 3744000; // i want to output: 43 days, 8 hours, 0 minutes
我必須創建一個函數來轉換呢?還是PHP已經有內置的東西來做到這一點,如date()
?
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);
接近http:// stackoverflow。 com/a/4798608/138383 –
從一些舊的代碼中得到它 - 但它最初可能來自Google上的同一個源代碼。 –
「bcmod」(它將字符串作爲參數並在編譯PHP時需要'--enable-bcmath')而不是標準模運算符'%'的任何原因? (http://uk.php.net/manual/en/language.operators.arithmetic.php) – IBBoard
這是非常簡單和容易找到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";
您可以輕鬆地用做['日期()'函數(HTTP: //php.net/manual/en/function.date.php) – Lix
我試過日期(),但它不會計算超過31天。我做錯了什麼? – supercoolville
@supercoolville你沒有做錯任何事,'date()'不會做你想做的事。 –