我正在试图弄清楚如何将一个包含秒数的变量格式化为一个字符串,该字符串会以人类可读的格式表示时间,但会将分钟数转换为下一个15。
示例:
$seconds = 4320; //
echo $convertAndRoundTime($seconds);
// would result in
//0 days, 1 hour and 15 minutes
// rather than
//0 days, 1 hour and 12 minutes
我可以使用以下代码(找到here)让它在确切的时间内工作,例如12分钟。
function secondsToTime($seconds) {
$dtF = new \DateTime('@0');
$dtT = new \DateTime("@$seconds");
return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds');
}
但是我怎样才能将分钟计时到下一个15分钟。所以如果是8分钟,则是15分钟,如果是18分钟到30分钟等等。
更新
我想我明白了......
$seconds = "4320";
$seconds = round($seconds);
if (gmdate("i",$seconds) % 15 != 0) $seconds = round($seconds / (15 * 60)) * (15 * 60);
$dtF = new \DateTime('@0');
$dtT = new \DateTime("@$seconds");
$days = $dtF->diff($dtT)->format('%a') * 3; // calculate days as 8 hour work days
echo $days.' days, '.$dtF->diff($dtT)->format('%h hours, %i minutes and %s seconds');
答案 0 :(得分:1)
将$seconds
除以900(15 * 60)舍入结果,再将其乘以900并将其分配给$seconds
。然后根据需要进行格式化。
例如:
$seconds = 4320;
$seconds = ceil($seconds / 900) * 900;
答案 1 :(得分:1)
使用@Girish的方法概念和@abcdn的舍入思想,我创建了下一个使它成为的代码
class MyTimeFormatter
{
const DAY_SECONDS = 86400; // 60*60*24;
const HOUR_SECONDS = 3600; // 60*60;
const MINUTE_SECONDS = 60; // 60;
public static function format($timestamp, $isQuarterMinutes = true)
{
$days = self::getTimeInDays($timestamp);
$timestamp -= $days * self::DAY_SECONDS;
$hours = self::getTimeInHours($timestamp);
$timestamp -= $hours * self::HOUR_SECONDS;
$minutes = self::getTimeInMinutes($timestamp);
$timestamp -= $minutes * self::MINUTE_SECONDS;
$seconds = $timestamp;
if ($isQuarterMinutes) {
$minutes = self::_getMinutesInQuarter($minutes);
}
return sprintf(
'%d day(s), %d hour(s), %d minute(s) and %d seconds',
$days, $hours, $minutes, $seconds
);
}
public static function getTimeInDays($timestamp)
{
return (int)($timestamp/self::DAY_SECONDS);
}
public static function getTimeInHours($timestamp)
{
return (int)($timestamp/self::HOUR_SECONDS);
}
public static function getTimeInMinutes($timestamp)
{
return (int)($timestamp/self::MINUTE_SECONDS);
}
private static function _getMinutesInQuarter($minutes)
{
return ceil($minutes/15) * 15;
}
}
所以如果你打电话
$seconds = 4322;
echo MyTimeFormatter::format($seconds), "\n";
echo MyTimeFormatter::format($seconds, false), "\n";
打印
0 day(s), 1 hour(s), 15 minute(s) and 2 seconds
0 day(s), 1 hour(s), 12 minute(s) and 2 seconds
答案 2 :(得分:0)
以下是答案:
<?php
$seconds = 93816;
echo getDaysTime($seconds);
function getDaysTime($seconds) {
$noOfDays = intval($seconds/86400);
$remainingSeconds = $seconds%86400;
return $noOfDays . ' day(s), ' . getHoursTime($remainingSeconds);
}
function getHoursTime($seconds) {
$noOfHours = intval($seconds/3600);
$remainingSeconds = $seconds%3600;
return $noOfHours . ' hour(s), ' . getMinutesTime($remainingSeconds);
}
function getMinutesTime($seconds) {
$noOfMinutes = intval($seconds/60);
$remainingSeconds = $seconds%60;
return $noOfMinutes . ' minute(s), ' . getSecondsTime($remainingSeconds);
}
function getSecondsTime($seconds) {
return $seconds . ' second(s), ';
}
?>