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
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.
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.
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.
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.
<?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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With