I have a variable called $final_time_saving
which 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小时10分钟
12 个解决方案
#1
90
<?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
#2
72
echo date('H:i', mktime(0,257));
#3
27
$hours = floor($final_time_saving / 60);
$minutes = $final_time_saving % 60;
#4
8
You can achieve this with DateTime
extension, 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');
演示
#5
7
@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 = 0
since 0:00 is better than 24:00.
我将代码更改为接受$ time = 0,因为0:00优于24:00。
One more thing - there is no case when $time
is 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);
}
#6
2
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.
很抱歉提出了一个旧话题,但我从其中一个答案中使用了一些代码,今天我告诉自己我可以不偷某人的代码就可以做到。我很惊讶它是多么容易。我想要的是在08:30返回510分钟,所以这就是代码所做的。
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.
我使用短变量名,因为我将使用该函数很多,而且我很懒。
#7
2
$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分钟
#8
1
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
#9
0
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
#10
0
The easiest way is :
最简单的方法是:
gmdate('H:i', $numberOfSeconds * 60)
#11
0
$m = 250;
$extraIntH = intval($m/60);
$extraIntHs = ($m/60); // float value
$whole = floor($extraIntHs); // return int value 1
$fraction = $extraIntHs - $whole; // Total - int = . decimal value
$extraIntHss = ($fraction*60);
$TotalHoursAndMinutesString = $extraIntH."h ".$extraIntHss."m";
#12
0
Thanks to @Martin_Bean and @Mihail Velikov answers. I just took their answer snippet and added some modifications to check,
感谢@Martin_Bean和@Mihail Velikov的回答。我刚刚拿了他们的答案片段并添加了一些修改来检查,
-
If only Hours only available and minutes value empty, then it will display only hours.
如果只有小时可用且分钟值为空,则它将仅显示小时数。
-
Same if only Minutes only available and hours value empty, then it will display only minutes.
如果仅“分钟”可用且小时值为空,则相同,则仅显示分钟。
-
If minutes = 60, then it will display as 1 hour. Same if minute = 1, the output will be 1 minute.
如果分钟= 60,则显示为1小时。如果分钟= 1,则相同,输出将为1分钟。
Changes and edits are welcomed. Thanks. Here is the code.
欢迎进行更改和编辑。谢谢。这是代码。
function convertToHoursMins($time) {
function convertToHoursMins($ time){
$hours = floor($time / 60);
$minutes = ($time % 60);
if($minutes == 0){
if($hours == 1){
$output_format = '%02d hour ';
}else{
$output_format = '%02d hours ';
}
$hoursToMinutes = sprintf($output_format, $hours);
}else if($hours == 0){
if ($minutes < 10) {
$minutes = '0' . $minutes;
}
if($minutes == 1){
$output_format = ' %02d minute ';
}else{
$output_format = ' %02d minutes ';
}
$hoursToMinutes = sprintf($output_format, $minutes);
}else {
if($hours == 1){
$output_format = '%02d hour %02d minutes';
}else{
$output_format = '%02d hours %02d minutes';
}
$hoursToMinutes = sprintf($output_format, $hours, $minutes);
}
return $hoursToMinutes;
}`
#1
90
<?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
#2
72
echo date('H:i', mktime(0,257));
#3
27
$hours = floor($final_time_saving / 60);
$minutes = $final_time_saving % 60;
#4
8
You can achieve this with DateTime
extension, 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');
演示
#5
7
@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 = 0
since 0:00 is better than 24:00.
我将代码更改为接受$ time = 0,因为0:00优于24:00。
One more thing - there is no case when $time
is 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);
}
#6
2
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.
很抱歉提出了一个旧话题,但我从其中一个答案中使用了一些代码,今天我告诉自己我可以不偷某人的代码就可以做到。我很惊讶它是多么容易。我想要的是在08:30返回510分钟,所以这就是代码所做的。
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.
我使用短变量名,因为我将使用该函数很多,而且我很懒。
#7
2
$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分钟
#8
1
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
#9
0
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
#10
0
The easiest way is :
最简单的方法是:
gmdate('H:i', $numberOfSeconds * 60)
#11
0
$m = 250;
$extraIntH = intval($m/60);
$extraIntHs = ($m/60); // float value
$whole = floor($extraIntHs); // return int value 1
$fraction = $extraIntHs - $whole; // Total - int = . decimal value
$extraIntHss = ($fraction*60);
$TotalHoursAndMinutesString = $extraIntH."h ".$extraIntHss."m";
#12
0
Thanks to @Martin_Bean and @Mihail Velikov answers. I just took their answer snippet and added some modifications to check,
感谢@Martin_Bean和@Mihail Velikov的回答。我刚刚拿了他们的答案片段并添加了一些修改来检查,
-
If only Hours only available and minutes value empty, then it will display only hours.
如果只有小时可用且分钟值为空,则它将仅显示小时数。
-
Same if only Minutes only available and hours value empty, then it will display only minutes.
如果仅“分钟”可用且小时值为空,则相同,则仅显示分钟。
-
If minutes = 60, then it will display as 1 hour. Same if minute = 1, the output will be 1 minute.
如果分钟= 60,则显示为1小时。如果分钟= 1,则相同,输出将为1分钟。
Changes and edits are welcomed. Thanks. Here is the code.
欢迎进行更改和编辑。谢谢。这是代码。
function convertToHoursMins($time) {
function convertToHoursMins($ time){
$hours = floor($time / 60);
$minutes = ($time % 60);
if($minutes == 0){
if($hours == 1){
$output_format = '%02d hour ';
}else{
$output_format = '%02d hours ';
}
$hoursToMinutes = sprintf($output_format, $hours);
}else if($hours == 0){
if ($minutes < 10) {
$minutes = '0' . $minutes;
}
if($minutes == 1){
$output_format = ' %02d minute ';
}else{
$output_format = ' %02d minutes ';
}
$hoursToMinutes = sprintf($output_format, $minutes);
}else {
if($hours == 1){
$output_format = '%02d hour %02d minutes';
}else{
$output_format = '%02d hours %02d minutes';
}
$hoursToMinutes = sprintf($output_format, $hours, $minutes);
}
return $hoursToMinutes;
}`