回答
$seconds = 559;
$minutes = floor($seconds/60); // 9
$seconds = $seconds % 60; // 19
我想你的意思是'$分鐘= floor($秒/ 60);'因爲它不是Python? – Xaerxess 2011-06-02 05:48:55
啊,我想我可以,謝謝 – 2011-06-02 18:13:49
見http://dev.kafol.net/2008/09/php-calculating-time-span-duration.html
$days = floor($seconds/60/60/24);
$hours = $seconds/60/60%24;
$mins = $seconds/60%60;
$secs = $seconds%60;
$duration='';
if($days>0) $duration .= "$days days ";
if($hours>0) $duration .= "$hours hours ";
if($mins>0) $duration .= "$mins minutes ";
if($secs>0) $duration .= "$secs seconds ";
下面是一個PHP版本,我做了隨機附帶AMX Mod X一些的SmallC本地代碼。
<?php
/**
* PHPInSimMod - Time Module
* @package PRISM
* @subpackage Time
* @copyright the AMX Mod X Development Team & The PRISM Devlopment Team.
*/
class Time
{
// Time unit types.
const UNIT_SECONDS = 0;
const UNIT_MINUTES = 1;
const UNIT_HOURS = 2;
const UNIT_DAYS = 3;
const UNIT_WEEKS = 4;
// The number of seconds that are in each time unit.
const SECONDS_IN_MINUTE = 60;
const SECONDS_IN_HOUR = 3600;
const SECONDS_IN_DAY = 86400;
const SECONDS_IN_WEEK = 604800;
/**
* @desc: By Brad for AMX Mod X.
* @param: Unit - The number of time units you want translated into verbose text.
* @param: Type - The type of unit (i.e. seconds, minutes, hours, days, weeks) that you are passing in.
*/
static function getLength($unit, $type = Time::UNIT_SECONDS)
{
# Ensure the varables are of the correct datatype.
$unit = (int) $unit;
$type = (int) $type;
# If our units happens to equal zero, skip.
if ($unit == 0)
return '0 seconds';
# Determine the number of each time unit there are.
$weeks = 0; $days = 0; $hours = 0; $minutes = 0; $seconds = 0;
# Handle the various scales of time.
switch ($type)
{
case Time::UNIT_SECONDS: $seconds = $unit;
case Time::UNIT_MINUTES: $seconds = $unit * Time::SECONDS_IN_MINUTE;
case Time::UNIT_HOURS: $seconds = $unit * Time::SECONDS_IN_HOUR;
case Time::UNIT_DAYS: $seconds = $unit * Time::SECONDS_IN_DAY;
case Time::UNIT_WEEKS: $seconds = $unit * Time::SECONDS_IN_WEEK;
}
# How many weeks left?
$weeks = $seconds/Time::SECONDS_IN_WEEK;
$seconds -= ($weeks * Time::SECONDS_IN_WEEK);
# How many days left?
$days = $seconds/Time::SECONDS_IN_DAY;
$seconds -= ($days * Time::SECONDS_IN_DAY);
# How many hours left?
$hours = $seconds/Time::SECONDS_IN_HOUR;
$seconds -= ($hours * Time::SECONDS_IN_HOUR);
# How many minutes left?
$minutes = $seconds/Time::SECONDS_IN_MINUTE;
$seconds -= ($minutes * Time::SECONDS_IN_MINUTE);
# Seconds are the base unit, so it's handled intrinsically
// Translate the unit counts into verbose text
$timeElement = array();
if ($weeks > 0)
$timeElement[] = sprintf("%i %s", $weeks, ($weeks == 1) ? "week" : "weeks");
if ($days > 0)
$timeElement[] = sprintf("%i %s", $days, ($days == 1) ? "day" : "days");
if ($hours > 0)
$timeElement[] = sprintf("%i %s", $hours, ($hours == 1) ? "hour" : "hours");
if ($minutes > 0)
$timeElement[] = sprintf("%i %s", $minutes, ($minutes == 1) ? "minute" : "minutes");
if ($seconds > 0)
$timeElement[] = sprintf("%i %s", $seconds, ($seconds == 1) ? "second" : "seconds");
// Outputs are final result in the correct format.
switch(count($timeElement))
{
case 1: return sprintf("%s", $timeElement[0]);
case 2: return sprintf("%s & %s", $timeElement[0], $timeElement[1]);
case 3: return sprintf("%s, %s & %s", $timeElement[0], $timeElement[1], $timeElement[2]);
case 4: return sprintf("%s, %s, %s & %s", $timeElement[0], $timeElement[1], $timeElement[2], $timeElement[3]);
case 5: return sprintf("%s, %s, %s, %s & %s", $timeElement[0], $timeElement[1], $timeElement[2], $timeElement[3], $timeElement[4]);
}
}
}
?>
你可以這樣做:
$sec = 559;
$min = floor($sec/60);
$sec = $sec % 60; // 19
我對格式化表示歉意,目前我在iPhone上。 – 2011-06-02 05:34:22
唯一的缺點使用gmdate("H:i:s",$foo)
的是,它僅適用於0和86399之間$foo
和它給你前導零,你可能會或可能不希望他們。下面是我使用的解決方案,它僅使用gmdate()
當我們的時間跨度很大:如果你想輸出更喜歡1 hr, 1 min, 7 sec
,你可以
echo formatTimeSpan(3); // 3 sec
echo formatTimeSpan(59); // 59 sec
echo formatTimeSpan(60); // 1 min, 0 sec
echo formatTimeSpan(559); // 9 min, 19 sec
echo formatTimeSpan(3667); // 1:01:07
echo formatTimeSpan(85483); // 23:44:43
echo formatTimeSpan(86901); // 1 d, 00:08:21
顯然,:
function formatTimeSpan($span) {
if ($span < (60)) { //--- less than a minute
$r = $span." sec"; # just use the seconds as they are
}
elseif ($span < (60*60)) { //--- a minute to under an hour
$min = intval($span/60);
$sec = $span - ($min*60);
$r = $min." min, ".$sec." sec";
}
elseif ($span < (24*60*60)) { //--- an hour to under a day
$r = gmdate("G:i:s",$span);
}
else { //--- a day or more
$days = intval($span/(24*60*60));
$remSec = $span - ($days * (24*60*60));
$r = $days." d, ".gmdate("G:i:s",$remSec);
}
return $r;
}
用法/輸出如下完全取出gmdate()
並修改格式/字符串以適合您的口味/需求。
小告誡,但:這並不檢查以查看是否「單位」應該是單數或複數,因此,我使用的縮寫如「秒」,「分鐘」,等另外,它不不檢查任何單位是否有0;你可能更願意將1 min, 0 sec
簡單地表示爲1 min
。
- 1. 如何將「'n'min'm'secs」轉換爲秒格式
- 2. 將秒轉換爲時間格式
- 3. PHP將秒轉換爲美分格式
- 4. Javascript - 將日期格式轉換爲秒
- 5. 將毫秒轉換爲小時格式
- 6. 將MySQL日期時間轉換爲非常具體的格式以便導出
- 7. 是否有將現有結構轉換爲腳本的便捷方式?
- 8. 的jQuery秒轉換爲這種格式
- 9. MySQL - 無法將時間格式轉換爲秒
- 10. 更有效的毫秒轉換方法?
- 11. Android:便捷方法將arraylist <object>轉換爲arraylist <element>?
- 12. PHP將ISO日期轉換爲更具可讀性的格式?
- 13. 將內聯格式化添加到用法的便捷方式消息
- 14. 轉換列表以JSON格式 - 快速簡便的方法
- 15. 如何轉換秒分:秒格式
- 16. Python中的便捷方法
- 17. 程序將時間以秒爲單位轉換爲hh:mm:ss格式
- 18. 如何將時間格式爲'00:04:13.67'轉換爲秒?
- 19. 轉換DateTime格式爲varchar格式式樣的解決方法
- 20. 將時間轉換爲秒+毫秒秒
- 21. 將HH:TT:SS格式的時間戳轉換爲秒
- 22. Php將秒轉換爲只讀值的可讀格式
- 23. 將時間格式mm.ss.dd轉換爲MS Excel中的秒數
- 24. 如何將毫秒數轉換爲iso格式的javascript?
- 25. 將毫秒轉換爲適當的java日期格式?
- 26. 如何將毫秒轉換爲Oracle中的hh mm ss格式
- 27. 如何將日期格式轉換爲postgresql中的毫秒數?
- 28. 將ISO 8601格式的時間段轉換爲ticks /毫秒
- 29. 將毫秒數轉換爲Rails中的格式化日期
- 30. 將HH:MM:SS格式的時間轉換爲僅秒?
除以60 :) – Ibu 2011-06-02 05:25:29
也使用模數'%' – Ibu 2011-06-02 05:26:17