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

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);

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>