Home >Backend Development >PHP Tutorial >How to Convert Minutes into Hours and Minutes in PHP?
Convert Minutes into Hours and Minutes with Precision in PHP
In PHP programming, when working with time intervals, it often becomes necessary to convert a number of minutes into a user-friendly format that displays hours and minutes separately. This allows for a more intuitive understanding of time duration.
Problem:
Suppose you have a PHP variable, let's call it $final_time_saving, which represents a number of minutes, such as 250. The challenge is to convert this numerical duration into a human-readable format like "4 hours 10 minutes."
Solution:
To achieve this conversion, PHP offers a simple and efficient solution:
<code class="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'); // outputs "4 hours 10 minutes"</code>
Explanation:
Example Output:
When you call the above function with $final_time_saving = 250, it will output "4 hours 10 minutes."
The above is the detailed content of How to Convert Minutes into Hours and Minutes in PHP?. For more information, please follow other related articles on the PHP Chinese website!