15
A
回答
30
基本上,這樣的:
echo date("H:i:s",$endtime-$starttime);
$endtime-$starttime
給出秒的持續時間,date用於格式化的輸出。聽起來像保存和讀取數據庫不是你的問題,所以我把它留在我的例子中。請注意,您必須使用microtime(true)
才能正常工作,使用空間分離的輸出microtime()
您無法輕鬆進行計算。
編輯:你可能也做你自己的所有計算。這只是基本的數學是這樣的:
$duration = $endtime-$starttime;
$hours = (int)($duration/60/60);
$minutes = (int)($duration/60)-$hours*60;
$seconds = (int)$duration-$hours*60*60-$minutes*60;
3
如果你有時間A
和時間B
,以秒計,那麼這兩項絕對時間之間的秒數爲:
B - A
如果你想要格式化此秒數,您可以使用date
來對其進行美化。
12
microtime to seconds or hours or min conversion?
microtime中是PHP功能,在微秒返回時間的措施,基本上微秒可以轉換爲名稱:
1 milliseconds = 1,000 microseconds 1 second = 1,000,000 microseconds 1 minute = 60,000,000 microseconds 1 hour = 3,600,000,000 microseconds or 1 microsecond = 0.001 milliseconds 1 microsecond = 0.000001 seconds 1 microsecond = 0.0000000166666667 minutes 1 microsecond = 0.000000000277777778 hours
3
function formatPeriod($endtime, $starttime)
{
$duration = $endtime - $starttime;
$hours = (int) ($duration/60/60);
$minutes = (int) ($duration/60) - $hours * 60;
$seconds = (int) $duration - $hours * 60 * 60 - $minutes * 60;
return ($hours == 0 ? "00":$hours) . ":" . ($minutes == 0 ? "00":($minutes < 10? "0".$minutes:$minutes)) . ":" . ($seconds == 0 ? "00":($seconds < 10? "0".$seconds:$seconds));
}
0
.heading {
color: #666;
border-bottom: 1px dotted #ddd;
font-weight: normal;
}
.intro {
color: #666;
background: #f5f5f5;
border: 1px solid #ccc;
border-radius: 2px;
padding: 10px;
}
<h1 class="heading">Someo Coole!</h1>
<p class="intro">I am a very little boy! Thanks for letting me do some stuff here and around here!</p>
<p>Sorry for taking place for none!</p>
function formatPeriod($endtime, $starttime)
{
$duration = $endtime - $starttime;
$hours = (int) ($duration/60/60);
$minutes = (int) ($duration/60) - $hours * 60;
$seconds = (int) $duration - $hours * 60 * 60 - $minutes * 60;
return ($hours == 0 ? "00":$hours) . ":" . ($minutes == 0 ? "00":($minutes < 10? "0".$minutes:$minutes)) . ":" . ($seconds == 0 ? "00":($seconds < 10? "0".$seconds:$seconds));
}
相關問題
- 1. C++轉換小時,分鐘,秒到秒
- 2. php - 轉換小時:分鐘:秒。秒
- 3. 使用javascript將時間轉換成分鐘或秒鐘
- 4. 將秒轉換爲小時和分鐘
- 5. 日期轉換 - 秒到小時/分鐘/秒
- 6. 如何將分鐘轉換成microtime
- 7. 將分鐘轉換爲小時,分鐘和秒
- 8. 小時和分鐘到秒
- 9. 小時,分鐘,秒到Time_t
- 10. 轉換時間int到分鐘秒小時
- 11. 轉換秒鐘到幾分鐘查詢
- 12. PHP/MySQL毫秒到小時:分鐘:秒
- 13. 將小時,分鐘,秒轉換爲小時字符串
- 14. 如何將秒轉換爲小時分鐘和秒?
- 15. 將秒轉換爲天:小時:分鐘:秒
- 16. 將毫秒轉換爲小時,分鐘和秒python
- 17. 如何將小時,分鐘和秒轉換爲秒
- 18. 如何將秒轉換爲小時:分鐘:秒在php
- 19. 將秒轉換爲小時,分鐘,seconds.hundreths第二秒
- 20. 使用sql或c將小時部分轉換爲分鐘#
- 21. 轉換毫秒到小時和分鐘紅寶石
- 22. 麻煩與轉換毫秒到小時和分鐘
- 23. SQL將秒數轉換爲分鐘數到小時數
- 24. 轉換分鐘時:分:秒在SQL
- 25. 將60分鐘或更長時間轉換爲小時
- 26. Python時間轉換器從秒到幾天,幾小時,幾分鐘和幾秒
- 27. 如何在SQL中將分鐘轉換爲小時分鐘和秒鐘?
- 28. microtime秒2小數
- 29. 如何將秒分別轉換爲年,月,日,小時,分鐘?
- 30. simpledateformat轉換24小時到分鐘
你的第一個示例並不總是有效。 'microtime'返回一個unix時間戳,顯然在UTC的1/1/70 00:00:00開始。如果'$ endtime - $ starttime'大於24小時,則返回的diff時間戳將代表UTC的2/1/70 01:00:00(假設它恰好爲25小時)。將此格式設置爲'H:i:s'會給出'01:00:00',這似乎是一個小時。 –
另外,可能值得在第二個例子中指出'microtime()'返回一個'timestamp微秒'的字符串 - 你不能對此做數學計算,所以你需要使用microtime(true)得到一個浮動 –
哦,感謝提示亞當,我沒有想到這一點,但你是對的:如果持續時間可能超過24小時,我的第一個解決方案將無法工作 - 一個將不得不添加「d」(和「m」和「Y」如果needet)輸出天數或手動進行計算(如我的第二個例子所示) – oezi