Convert minutes to hours

Asked

Viewed 2,898 times

-4

I need to convert two minutes values to hours using PHP.

valor1 = 278 min
valor2 = 1161 min
  • 3

    Split by 60 does not solve?

  • Has this function ready here https://stackoverflow.com/questions/8563535/convert-number-of-minutes-into-hours-minutes-using-php

  • 1

    I will give -1 because you do not ask clearly what you wanted and then change the question by invalidating answers made.

  • 1

    The question was this: "Hello, I need to convert two minutes values to hours using PHP."

  • @Pauloramos Pois eh. Ask a question in a way, we spend time to answer and then change the meaning of the question. It’s the same as asking: "How do you make sandwich with bread and burger?".. then it changes: "With bread, hamburger and ham?"... pitiful

  • When are you asked what time it is? You answer only the hour, or you answer the minutes tbm?

  • This is not the case. We are talking about programming.

  • If you have another question with what you want, you can do, there’s no problem in it. You can’t change the meaning of the question.

  • Anyway, I don’t only want to know the time. I believe that this is my question and that of many in this forum. Turning minutes into h:i searched on several forums... All unsolved. I’m sorry if I wasn’t clear, but the idea from the start was.

  • You didn’t ask for minutes, you asked for hours. It’s pretty clear in the question. If you made a mistake, fine, no problem, but you can’t change the question by invalidating answers already made.

  • You could change the question if there were no answers. It is normal to be wrong, but in this case after 4 hours change the question when there are answers, it is not possible. If you wanted in h:m, have an answer there that suits you well.

  • OK could indicate which one?

  • There are only two answers. Mine and another user.

  • No answer solves the values from minutes to hours. I will open a new question.

  • Wow, so why did you dial the answer? We only dial the answer when it solves the problem. When it does not solve, you can question in the comments of the answer.

  • Solved the question I made, not the one edited.

Show 11 more comments

2 answers

8


$minutos_totais = 278;
$horas = floor($minutos_totais / 60);
$minutos = $minutos_totais % 60;

php > $minutos_totais = 1439;
php > echo floor($minutos_totais / 60);
23
php > echo $minutos_totais % 60;
59
  • Test this way and did not work, I put in total minutes 1439, that would have to return 23:59 minutes in the case, returned 59.

  • 2

    @arllondias Your review doesn’t seem to make sense to me, since the standard understanding of "converting minutes to hours" is to just take an X amount of minutes and return a Y amount of hours. So 1439 minutes is 23 hours. Anyway Victor has already shown the calculation to know which "minutes are left".

  • @LINQ is right, before the question was edited, I got confused and printed only the result of the minute variable. it was just lack of attention, all right.

0

By the statement of the question, the intention is to convert the amount of minutes into hours, and not to return hours and minutes.

The correct one would only be to divide the minutes by 60, returning the value in hours with decimals (if the value is not multiple of 60):

<?php
$valor1 = 278;
$valor2 = 1161;

// arredondando para 2 casas decimais
$valor1 = round($valor1 / 60, 2); // retorna 4.63
$valor2 = round($valor2 / 60, 2); // retorna 19.35
?>

That is to say, 278 minutes amounts to 4.63 hours, and 1161 minutes a 19.35 hours.

If you do not want the decimals (the remnant who did not complete 1 hour):

<?php
$valor1 = 278;
$valor2 = 1161;

// arredondando para 2 casas decimais
$valor1 = floor($valor1 / 60); // retorna 4
$valor2 = floor($valor2 / 60); // retorna 19
?>
  • It doesn’t solve this last one, if you’re going to add the two of them past 24:00.

  • @All right, it was just a complement to the first example.

  • I need you to convert the min p hours, display hours and minutes in h:i format. Summing the two values totals 23:59

  • @Lucasspielmann I get it. But the question doesn’t say that.

  • @Lucasspielmann The question only says convert minutes into hours

Browser other questions tagged

You are not signed in. Login or sign up in order to post.