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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8563535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:00:22  来源:igfitidea点击:

Convert number of minutes into hours & minutes using PHP

phptime

提问by Rob

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

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

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

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

4 hours 10 minutes

4 hours 10 minutes

回答by Martin Bean

<?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

回答by Alastair

echo date('H:i', mktime(0,257));

回答by Sjoerd

$hours = floor($final_time_saving / 60);
$minutes = $final_time_saving % 60;

回答by Glavi?

You can achieve this with DateTimeextension, which will also work for number of minutes that is larger than one day (>= 1440):

您可以通过DateTime扩展来实现这一点,它也适用于大于一天 ( >= 1440)的分钟数:

$minutes = 250;
$zero    = new DateTime('@0');
$offset  = new DateTime('@' . $minutes * 60);
$diff    = $zero->diff($offset);
echo $diff->format('%a Days, %h Hours, %i Minutes');

demo

demo

回答by Mihail Velikov

@Martin Bean's answer is perfectly correct but in my point of view it needs some refactoring to fit what a regular user would expect from a website (web system).
I think that when minutes are below 10 a leading zero must be added.
ex: 10:01, not 10:1

@Martin Bean 的回答是完全正确的,但在我看来,它需要进行一些重构以适应普通用户对网站(网络系统)的期望。
我认为当分钟数低于 10 时,必须添加前导零。
例如:10:01,而不是 10:1

I changed code to accept $time = 0since 0:00 is better than 24:00.

我更改了代码以接受,$time = 0因为 0:00 比 24:00 好。

One more thing - there is no case when $timeis bigger than 1439 - which is 23:59 and next value is simply 0:00.

还有一件事 - 没有$time大于 1439 的情况 - 即 23:59,下一个值只是 0:00。

function convertToHoursMins($time, $format = '%d:%s') {
    settype($time, 'integer');
    if ($time < 0 || $time >= 1440) {
        return;
    }
    $hours = floor($time/60);
    $minutes = $time%60;
    if ($minutes < 10) {
        $minutes = '0' . $minutes;
    }
    return sprintf($format, $hours, $minutes);
}

回答by Mike

$t = 250;
$h = floor($t/60) ? floor($t/60) .' hours' : '';
$m = $t%60 ? $t%60 .' minutes' : '';
echo $h && $m ? $h.' and '.$m : $h.$m;

4 hours and 10 minutes

4小时10分钟

回答by Lurvik

Sorry for bringing up an old topic, but I used some code from one of these answers a lot, and today I told myself I could do it without stealing someone's code. I was surprised how easy it was. What I wanted is 510 minutes to be return as 08:30, so this is what the code does.

很抱歉提出了一个旧话题,但我经常使用其中一个答案中的一些代码,今天我告诉自己我可以在不窃取某人代码的情况下做到这一点。我很惊讶这是多么容易。我想要的是 510 分钟作为 08:30 返回,所以这就是代码的作用。

function tm($nm, $lZ = true){ //tm = to military (time), lZ = leading zero (if true it returns 510 as 08:30, if false 8:30
  $mins = $nm % 60;
  if($mins == 0)    $mins = "0$mins"; //adds a zero, so it doesn't return 08:0, but 08:00

  $hour = floor($nm / 60);

  if($lZ){
    if($hour < 10) return "0$hour:$mins";
  }

  return "$hour:$mins";
}

I use short variable names because I'm going to use the function a lot, and I'm lazy.

我使用短变量名是因为我会经常使用这个函数,而且我很懒。

回答by Seif.ben

The easiest way is :

最简单的方法是:

    gmdate('H:i', $numberOfSeconds * 60)

回答by kelaskakap

Just in case you want to something like:

以防万一你想要这样的东西:

echo date('G \h\o\u\r\s i \m\i\n\u\t\e\s', mktime(0, 90)); //will return 1 hours 30 minutes
echo date('G \j\a\m i \m\e\n\i\t', mktime(0, 90)); //will return 1 jam 30 menit

回答by HIR

function hour_min($minutes){// Total
   if($minutes <= 0) return '00 Hours 00 Minutes';
else    
   return sprintf("%02d",floor($minutes / 60)).' Hours '.sprintf("%02d",str_pad(($minutes % 60), 2, "0", STR_PAD_LEFT)). " Minutes";
}
echo hour_min(250); //Function Call will return value : 04 Hours 10 Minutes