请问怎么使用PHP将分钟数转换为小时和分钟

我有一个称为$final_time_saving分钟的变量,例如250分钟。


如何使用以下格式的PHP将分钟数转换为小时数和分钟数:


4 hours 10 minutes


小唯快跑啊
浏览 308回答 3
3回答

不负相思意

<?phpfunction convertToHoursMins($time, $format = '%02d:%02d') {&nbsp; &nbsp; if ($time < 1) {&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; $hours = floor($time / 60);&nbsp; &nbsp; $minutes = ($time % 60);&nbsp; &nbsp; return sprintf($format, $hours, $minutes);}echo convertToHoursMins(250, '%02d hours %02d minutes'); // should output 4 hours 17 minutes

繁星点点滴滴

您可以通过DateTime扩展来实现此目的,该扩展名也可以在大于一天(>= 1440)的分钟数内工作:$minutes = 250;$zero&nbsp; &nbsp; = new DateTime('@0');$offset&nbsp; = new DateTime('@' . $minutes * 60);$diff&nbsp; &nbsp; = $zero->diff($offset);echo $diff->format('%a Days, %h Hours, %i Minutes');

隔江千里

它需要进行一些重构,以适应普通用户对网站(网络系统)的期望。我认为,当分钟数低于10时,必须添加一个前导零。例如:10:01,而不是10:1我将代码更改为接受,$time = 0因为0:00优于24:00。还有一件事-不可能$time大于1439-23:59,下一个值就是0:00。function convertToHoursMins($time, $format = '%d:%s') {&nbsp; &nbsp; settype($time, 'integer');&nbsp; &nbsp; if ($time < 0 || $time >= 1440) {&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; $hours = floor($time/60);&nbsp; &nbsp; $minutes = $time%60;&nbsp; &nbsp; if ($minutes < 10) {&nbsp; &nbsp; &nbsp; &nbsp; $minutes = '0' . $minutes;&nbsp; &nbsp; }&nbsp; &nbsp; return sprintf($format, $hours, $minutes);}
打开App,查看更多内容
随时随地看视频慕课网APP