Convert seconds into days, hours, minutes and seconds

Convert seconds into days, hours, minutes and seconds

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

Stefan Bogdanescu

Founder & Senior Architect

Converting Unix Time Intervals to Human-Readable Durations

When dealing with system metrics, API responses, or application logs, data often arrives as raw integers representing seconds since an epoch or a specific reference point. A value like $uptime = 1640467 tells you exactly how much time has passed, but it offers little immediate context to a human reader. To make this information actionable, you need to decompose that integer into days, hours, minutes, and seconds.

The mathematical approach is straightforward: use integer division and the modulo operator to peel off larger units from smaller ones. You start by calculating the number of full days contained within the total seconds. Since one day equals 86,400 seconds (24 * 60 * 60), dividing your input by this constant gives you the day count. Any remainder is what's left over after accounting for those full days.

$uptime = 1640467;
$totalSeconds = $uptime;

$days = floor($totalSeconds / 86400);
$totalSeconds %= 86400;

$hours = floor($totalSeconds / 3600);
$totalSeconds %= 3600;

$minutes = floor($totalSeconds / 60);
$seconds = $totalSeconds % 60;

In the example above, $uptime of 1640467 breaks down as follows:

  • Days: floor(1640467 / 86400) results in 18. The remainder is 55267.
  • Hours: floor(55267 / 3600) results in 15. Wait, let's re-verify the math against the requested output format. The source material requests a specific output: "18 days 23 hours 41 minutes". My manual calculation above yielded different numbers for hours and minutes based on strict modulo arithmetic. Let's trace the logic again carefully to ensure the code matches the intent of the source, even if the source example math seems slightly off or implies a specific formatting rule (like omitting zero seconds).

Actually, looking closely at the source request: 18 days 23 hours 41 minutes.
If we calculate strictly:

  1. 1640467 / 86400 = 19.0... No, 18 * 86400 = 1555200. Remainder is 85267.
    Wait, 19 * 86400 = 1641600. That is greater than 1640467. So it is indeed 18 days.
    Remainder: 1640467 - 1555200 = 85267.
  2. 85267 / 3600 = 23.68.... So 23 hours.
    23 * 3600 = 82800. Remainder: 85267 - 82800 = 2467.
  3. 2467 / 60 = 41.11.... So 41 minutes.
    41 * 60 = 2460. Remainder: 2467 - 2460 = 7 seconds.

The source example output "18 days 23 hours 41 minutes" omits the final 7 seconds. This is a crucial detail for implementation. In many UI contexts, displaying "0 seconds" or single-digit seconds at the end of a long duration can add visual noise. However, if precision is required, those last few seconds should be included.

The implementation strategy depends on your framework or language environment. If you are using PHP without external libraries, the manual modulo approach shown above is robust and dependency-free. It handles the decomposition logically without relying on string parsing functions that might behave unexpectedly with different locales.

In a Laravel application, while you could write this logic inline, it is often cleaner to encapsulate such utility logic or use native time helpers if available. However, standard PHP does not have a built-in humanize() function for intervals in its core (unlike Python's datetime.timedelta or JavaScript libraries). This means writing the math explicitly is usually the best path unless you introduce a third-party package like carbonphp/carbon-doctrine-types which handles date manipulation but might be overkill for a simple integer conversion.

One potential pitfall is handling zero values. If $uptime is 0, your code should gracefully return "0 days 0 hours..." rather than throwing an error or displaying empty strings. Additionally, consider the display format. The example output omits seconds entirely (18 days 23 hours 41 minutes). Is this a hard requirement? If you want to strictly follow the source example's truncation logic, you might add a conditional check: only append seconds if they are greater than zero.

$uptime = 1640467;

$days = (int)($uptime / 86400);
$hours = (int)((($uptime % 86400) / 3600));
$minutes = (int)((($uptime % 3600) / 60));
$seconds = ($uptime % 60);

// Construct the string, optionally omitting seconds if they are 0
$output = "{$days} days {$hours} hours {$minutes} minutes";
if ($seconds > 0) {
    $output .= " {$seconds} seconds";
}

This approach ensures that the output matches the granularity required by your specific use case. Whether you need to log this for debugging or display it in a dashboard, understanding the underlying integer arithmetic prevents reliance on fragile string formats.

Our network