如何将分钟转换为小时和分钟?例如
$dt = new DateTime();
$currDate = date_format($dt, 'Y-m-d');
$startMinutes = 939;
$startT = $currDate . " " . gmdate("H:i", $startMinutes);
此代码返回类似 '2012-09-12 00:15' 而不是 15:... 为什么?
date 和 gmdate 函数都以秒而不是分钟作为输入。所以你所要做的就是将你的 startminutes 乘以 60 来得到你的答案。
$startT = $currDate . " " . gmdate("H:i", ($startMinutes * 60));
类似以下公式的东西应该起作用:
$hours = floor($minutes/60); //round down to nearest minute.
$minutes = $minutes % 60;
非常快速的方法,这应该有效:
echo date('H:i', strtotime($startMinutes.' minutes'));
$dt = new DateTime('now');
$dt->modify('+939 minutes');
echo $dt->format('Y-m-d H:i');
试试这个..
$d = floor ($startMinutes / 1440);
$h = floor (($startMinutes - $d * 1440) / 60);
$m = $startMinutes - ($d * 1440) - ($h * 60);
echo "{$startMinutes}min converts to {$d}d {$h}h {$m}m";
你应该可以从那里拼凑起来..
试试这个
function hour_min($minutes){
if($minutes == 0){return '';}else{
$hours = round(intval($minutes/60) , 2); //round down to nearest minute.
$minutes = $minutes % 60; return sprintf("%02d", $hours).':'.sprintf("%02d", $minutes);
}
}
echo hour_min(720); // 12:00
echo hour_min(480); // 08:00