将分钟数转换为小时数分钟使用 PHP

2021-12-23 00:00:00 time php

我有一个名为 $final_time_saving 的变量,它只是一个分钟数,例如 250.

I have a variable called $final_time_saving which is just a number of minutes, 250 for example.

如何使用 PHP 将分钟数转换为小时和分钟:

How can I convert that number of minutes into hours and minutes using PHP in this format:

4 小时 10 分钟

推荐答案

<?php

function convertToHoursMins($time, $format = '%02d:%02d') {
    if ($time < 1) {
        return;
    }
    $hours = floor($time / 60);
    $minutes = ($time % 60);
    return sprintf($format, $hours, $minutes);
}

echo convertToHoursMins(250, '%02d hours %02d minutes'); // should output 4 hours 17 minutes

相关文章