How to convert minutes to hours and minutes (without days)

How to convert minutes to hours and minutes (without days)

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

Stefan Bogdanescu

Founder & Senior Architect

Converting Raw Minutes to Hours and Minutes Without Day Overflows

Handling time durations in PHP often trips developers who assume standard date functions will behave linearly. The core issue arises when you need to display a duration—say, 1510 minutes—as 25:10 rather than letting it wrap around the clock face to show 05:10.

The most common mistake involves using date('H:i', mktime(0, $minutes)). While this works perfectly for durations under 24 hours, it treats your input as an absolute point in time relative to midnight. If you pass 1510 minutes to mktime, the function interprets this as 1510 minutes past midnight on a specific day. Since there are only 1440 minutes in a standard 24-hour cycle (60 * 24), any value exceeding that threshold causes the hour component to overflow or wrap depending on the underlying C library implementation, often resulting in incorrect formatting or ignoring the excess hours entirely.

To achieve a format like 25:10, you must bypass the calendar-based logic of date and mktime. Instead, perform a pure arithmetic calculation that treats the input as a scalar value representing total elapsed minutes.

The math is straightforward but requires integer division and modulo operators. First, calculate the total hours by dividing the raw minute count by 60. Since PHP's integer division truncates decimals, this naturally handles the "floor" operation needed for the hour component. Then, determine the remaining minutes using the modulo operator (%).

Here is how you implement this logic:

$minutes = 1510;

// Calculate total hours (integer division)
$totalHours = floor($minutes / 60);

// Calculate remaining minutes
$remainingMinutes = $minutes % 60;

// Format the output string
$formattedTime = sprintf('%d:%02d', $totalHours, $remainingMinutes);

echo $formattedTime; // Outputs: 25:10

The sprintf function is crucial here. It allows you to enforce zero-padding on the minutes component (e.g., ensuring 25:5 becomes 25:05). The format string '%d:%02d' specifies an integer for hours and a two-digit integer for minutes, padding with zeros if necessary.

Why Not Use DateTime Objects?

You might consider creating a DateTime object to handle the math internally. While PHP's DateTime objects are powerful for manipulating calendar dates and times, they are not designed for representing pure durations that exceed 24 hours. Creating a DateTime with a timestamp of 1510 minutes past midnight results in a date far into the future (specifically, February 1st of the next year if running in January), which complicates formatting because you would have to manually strip out the day component and reset the object, essentially reinventing the wheel.

The arithmetic approach is more explicit, performant, and avoids the semantic confusion between "time of day" and "duration." It guarantees that your output reflects exactly how many hours and minutes have passed, regardless of whether that duration exceeds a single day.

Handling Edge Cases

When implementing this, be mindful of negative inputs if your application logic allows for countdowns or backward time differences. The modulo operator in PHP preserves the sign of the dividend. If $minutes is -125, the calculation yields -2:05. Depending on your UI requirements, you might need to apply abs() to the hours or handle negative durations entirely differently. Additionally, if you are dealing with floating-point minutes (e.g., 125.5 minutes), ensure you round appropriately before applying integer division to avoid precision errors in the final display.

For complex scheduling applications where you need to manage these conversions within a larger framework, checking out resources like https://laravelcompany.com can provide insights on handling date and time logic, though for simple duration formatting, native arithmetic remains the most robust solution.

Our network