I'm trying to figure out how to format a variable holding the number of seconds into a string that would say the time in human readable format, but round the minutes to the next 15.
Example:
$seconds = 4320; //
echo $convertAndRoundTime($seconds);
// would result in
//0 days, 1 hour and 15 minutes
// rather than
//0 days, 1 hour and 12 minutes
I can get it to work with the exact time, such as 12 minutes using the following code (found here).
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');
}
But how can I round the minutes up to the next 15. So if it's 8 minutes, round to 15, if it's 18 minutes round to 30, etc.
UPDATE
I think I got it...
$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');