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