Skip to content

Instantly share code, notes, and snippets.

@agarzon
Created July 11, 2012 14:24
Show Gist options
  • Save agarzon/3090691 to your computer and use it in GitHub Desktop.
Save agarzon/3090691 to your computer and use it in GitHub Desktop.
Calculate remaining days, hours, minutes and seconds from seconds
<?php
function secondsToTime($seconds) {
// Extract days
$days = floor($seconds / 86400);
// Extract hours
$divisorHours = $seconds % 86400;
$hours = floor($divisorHours / 3600);
// Extract minutes
$divisorMinutes = $seconds % 3600;
$minutes = floor($divisorMinutes / 60);
// Extract the remaining seconds
$divisorSeconds = $divisorMinutes % 60;
$seconds = floor($divisorSeconds);
$timeleft = array(
"d" => (int) $days,
"h" => (int) $hours,
"m" => (int) $minutes,
"s" => (int) $seconds,
);
return $timeleft;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment