2013-01-07 173 views
2

我的表中有總秒數值,但我想獲得一個時間格式,例如hh:mm:ss, 目前我有例如:seconds - 226,我知道時間格式應該是4分26秒秒,但我試過這段代碼:php將秒轉換爲時間戳

$seconds = 226; 
$hours = floor($seconds/3600); 
$mins = floor(($seconds - ($hours*3600))/60); 
$secs = floor(($seconds - ($hours*3600)) - ($mins*60)); 

這個輸出3:46

也許有一些錯誤的公式?

編輯:我接到了一個YouTube的腳本返回視頻的持續時間這個值:

$ytvidid = $url; 
$ytdataurl = "http://gdata.youtube.com/feeds/api/videos/". $ytvidid; 
$feedURL = $ytdataurl; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $feedURL); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
// get the result of http query 
$output = curl_exec($ch); 
curl_close($ch); 
// feed the curl output to simplexml_load_string 
$sxml = simplexml_load_string($output) or die("XML string not loading"); 

//$sxml = simplexml_load_file($feedURL); 
$media = $sxml->children('http://search.yahoo.com/mrss/'); 
// get <yt:duration> node for video length 
$yt = $media->children('http://gdata.youtube.com/schemas/2007'); 
$attrs = $yt->duration->attributes(); 
    echo $attrs['seconds']; 
+1

我不相信的! - 4 * 60 = +26 226 – swapnesh

+0

我只是說我是如何得到的秒數這就是我」 m比較值,所以它給我的秒數我想將其轉換回實時 – Andres

+0

我不明白與mysql有什麼關係。 –

回答

1

u可以使用這一個..這個代碼,如YouTube時

$total_secs = '15454'; 

//time settings 
$hours = floor($total_secs/3600); 
$mins = floor(($total_secs - ($hours*3600))/60); 
$secs = floor($total_secs % 60); 

//if hours zero, give for nothing like that (45:05)  
if ($hours<1) { $hours = ''; } else { $hours = $hours.':'; } 
if ($mins<10) { $mins = '0'.$mins.':'; } else { $mins = $mins.':'; } 
if ($secs<10) { $secs = '0'.$secs; } else { $secs = $secs; } 

echo $output = $hours.$mins.$secs; //