Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert number of minutes into hours & minutes using PHP

Tags:

php

time

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

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

4 hours 10 minutes

like image 882
Rob Avatar asked Dec 19 '11 15:12

Rob


People also ask

How do you convert from minutes to hours?

There are 60 minutes in 1 hour. To convert from minutes to hours, divide the number of minutes by 60. For example, 120 minutes equals 2 hours because 120/60=2.

How do you convert minutes to hours and seconds?

Converting between hours, minutes, and seconds using decimal time is relatively straightforward: time in seconds = time in minutes * 60 = time in hours * 3600. time in minutes = time in seconds / 60 = time in hours * 60. time in hours = time in minutes / 60 = time in seconds / 3600.

How do you write 15 minutes in hours?

In this case, if we convert 15 minutes to hours we write it as 15/60 because 1 hour = 60 minutes. This means 15 minutes can be written as 0.25 hours in the decimal form.

How many hours is 4 45 minutes?

To convert to decimal hours, add (minutes ÷ 60) to the hours number. So, 4 hours 45 minutes is 4 + 45 ÷ 60 = 4.75 hours. To convert to minutes, simply multiply the hours by 60 and add the minutes. So, 4 × 60 + 45 = 285 minutes.


1 Answers

<?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 
like image 83
Martin Bean Avatar answered Sep 21 '22 23:09

Martin Bean