Archive

Archive for July, 2009

Splitting a time value into day, hour, mins, secs in PHP

July 17th, 2009

I’ve been writing a new webpage that calculates the time left before a user license period expires. I wanted to display the time as a countdown of the number of days left before the license runs out. I couldn’t find an off-the-shelf PHP function to do the job so I quickly wrote the function below.

/* This function takes a PHP time value and returns the duration as an array
with 4 elements -
element 0 = days
element 1 = hours
element 2 = minutes
element 3 = seconds
*/
function DurationInDHMS($timeduration)
{
    $days = floor($timeduration / 86400);
    $timeduration = $timeduration - ($days * 86400);
    $hours = floor($timeduration / 3600);
    $timeduration = $timeduration - ($hours * 3600);
    $minutes = floor($timeduration / 60);
    $seconds = $timeduration - ($minutes * 60);
    return array($days, $hours, $minutes, $seconds);
}