Web Analytics Made Easy -
StatCounter Function to retrieve remaining time from two timestamps - CodingForum


No announcement yet.

Function to retrieve remaining time from two timestamps

  • Filter
  • Time
  • Show
Clear All
new posts

  • Function to retrieve remaining time from two timestamps

    After answering this post - http://www.codingforum.net/showthread.php?t=246280 - I thought it would be useful to a lot of people if I expanded the function a bit and posted it here.

    PHP Code:
    function getRemaining($now,$future){
    $future <= $now){
    // Time has already elapsed
    return FALSE;
    // Get difference between times
    $time $future $now;
    $minutesFloat $time/60;
    $minutes floor($minutesFloat);
    $hoursFloat $minutes/60;
    $hours floor($hoursFloat);
    $daysFloat $hours/24;
    $days floor($daysFloat);
            return array( 
    'days' => $days
    'hours' => round(($daysFloat-$days)*24),
    'minutes' => round(($hoursFloat-$hours)*60),
    'seconds' => round(($minutesFloat-$minutes)*60)

    This function returns the difference between two timestamps, separated into an array with indexes days, hours, minutes and seconds. For practical reasons, it will return FALSE if the time to be compared with $now is less than (or equal to) $now.

    For example, if you had a list of items in an auction and the database held a timestamp for when the auction ends, you would enter this timestamp as the second parameter to the function, and time() as the first parameter. Then, when the auction end timestamp is <= time(), the function returns false.
    PHP Code:
    $result getRemaining(time(),$auctionEnd);
    'Auction has ended!';
    // do what you want with $result array

    Hope it's useful, and all feedback welcomed
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    // With this
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.