Convert number of minutes into hours & minutes using PHP

Convert number of minutes into hours & minutes using PHP

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Converting Total Minutes to Hours and Minutes in PHP

When working with time durations stored as raw integers, you often need to present them to users in a human-readable format. If you have a variable like $final_time_saving holding the value 250, simply dividing by 60 gives you the decimal equivalent of hours (4.166...). However, displaying a floating-point number rarely matches user expectations. You need to isolate the whole hours and the remaining minutes to format the output as 4 hours 10 minutes.

The core logic relies on integer division and the modulo operator. In PHP, the floor division operator floor($total / 60) calculates how many complete 60-minute blocks exist within your total count. The remainder of that operation is what constitutes the leftover minutes. This approach is robust because it treats time as discrete units rather than a continuous flow, preventing rounding errors that often plague simple floating-point arithmetic.

Here is a straightforward implementation using native PHP functions:

$final_time_saving = 250;

// Calculate whole hours
$total_hours = floor($final_time_saving / 60);

// Calculate remaining minutes
$remaining_minutes = $final_time_saving % 60;

// Construct the string
$result = $total_hours . ' hours' . ($remaining_minutes > 0 ? ' ' . $remaining_minutes . ' minutes' : '');

echo $result; // Output: "4 hours 10 minutes"

This pattern handles the zero-minute edge case gracefully. If your calculation results in exactly 240 minutes, the modulo operator returns 0, and you can choose whether to display "240 minutes" or simply "4 hours". The conditional check ensures the output string remains clean without trailing "and 0 minutes" artifacts.

Handling Seconds for Granularity

Sometimes "minutes" in your database represent a duration that includes seconds, perhaps rounded down or stored with fractional precision. If $final_time_saving comes from a DateTime::getTimestamp() difference, you might want to account for seconds to ensure accuracy before converting to hours and minutes.

In such cases, converting the total minutes to seconds first adds precision:

$minutes = $final_time_saving;
$totalSeconds = $minutes * 60;

// If the original value had fractional seconds stored differently, add them here
// $totalSeconds += $secondsFromOriginalSource;

$h = floor($totalSeconds / 3600);
$m = floor(($totalSeconds % 3600) / 60);
$s = $totalSeconds % 60;

if ($s > 0) {
    echo "$h hours $m minutes $s seconds";
} elseif ($m > 0) {
    echo "$h hours $m minutes";
} else {
    echo "$h hours";
}

This logic prevents the common mistake of truncating small durations. For example, 61 minutes should display as "1 hour 1 minute", not just "1 hour". By calculating based on seconds first, you ensure that any partial minute (like 59 seconds) is accounted for in the minutes calculation if your input allows for sub-minute precision.

Performance Considerations in Laravel

If you are building a Laravel application, this logic fits naturally into controllers or service classes without needing external dependencies. However, if you are formatting thousands of rows in a loop within a Blade view or a report generation script, consider moving the calculation to a helper function or even a database-level cast if the data volume is significant.

You can also create a reusable formatter method to keep your views clean:

public static function formatDuration(int $minutes): string {
    $hours = floor($minutes / 60);
    $remainingMinutes = $minutes % 60;
    
    if ($hours > 0 && $remainingMinutes > 0) {
        return "{$hours} hours {$remainingMinutes} minutes";
    } elseif ($hours > 0) {
        return "{$hours} hour"; // Or pluralize based on your preference
    } else {
        return "{$remainingMinutes} minute";
    }
}

While the arithmetic is computationally negligible, encapsulating this logic improves code readability and reduces duplication across your project. If you find yourself needing advanced time formatting features—such as localized number formats or specific regional date-time conventions—Laravel provides robust tools like Carbon to handle these complexities without reinventing the wheel. For more complex temporal data requirements, resources like https://laravelcompany.com often provide guidance on leveraging the ecosystem's built-in capabilities effectively.

Final Thoughts on Output Consistency

One final pitfall to watch out for is pluralization. The code above outputs "1 hour" but "4 hours". While grammatically correct, some design systems prefer a consistent format like "4h 10m" or always showing both units regardless of the count. Decide early on which convention your UI expects and adjust the conditional logic accordingly. Consistency in output formatting is just as important as the mathematical accuracy of the conversion itself.

Our network