• home
  • forum
  • my
  • kt
  • download
  • Show time left with PHP, nice formatted

    Author: 2007-08-11 12:32:38 From:

    Let say you have a time or date in the future, and want to show your visitor the time left until that time.

    I want to show something like:

    • 1 Day(s), 5 Hours
    • 4 Hour(s), 35 Minute(s)
    • 5 Second(s)

    The function must be able to output any of these examples. The first example probably have some minutes and seconds aswell left, but they are not interesting so we only show days and hours. The same way if we are down ad hours, then we only show hours and minutes and so on...

    function timeLeft($theTime)
    {
    $now = strtotime("now");
    $timeLeft = $theTime - $now;

    if($timeLeft > 0)
    {
    $days = floor($timeLeft/60/60/24);
    $hours = $timeLeft/60/60%24;
    $mins = $timeLeft/60%60;
    $secs = $timeLeft%60;

    if($days)
    {
    $theText = $days . " Day(s)";
    if($hours)
    {
    $theText .= ", " .$hours . " Hour(s) ";
    }
    }
    elseif($hours)
    {
    $theText = $hours . " Hour(s)";
    if($mins)
    {
    $theText .= ", " .$mins . " Minute(s) ";
    }
    }
    elseif($mins)
    {
    $theText = $mins . " Minute(s)";
    if($secs)
    {
    $theText .= ", " .$secs . " Second(s) ";
    }
    }
    elseif($secs)
    {
    $theText = $secs . " Second(s)";
    }
    }
    else
    {
    $theText = "The time is already in the past!";
    }

    return $theText;
    }

     

    The first part is "only" some math to seperate the days, hours, minutes and seconds (left). If it's one day and two seconds, we get back $days = 1, $hours = 0, $minutes = 0, $seconds = 2. The techniques used is called div and mod (/ and % operators). The give you back either how many whole times you can dived or the rest after a divition. Anyway, they are great for getting this information :-) don't bother if you don't care...

    What I do after that is to check if we have any days and in that case write the number of days, check if we have minutes and if that is the case write them aswell.

    If we didn't have any days, we do a check to see if we have any hours... and so on and so on. the last way we can take is when we only have seconds left. Then we show only the seconds.

    (Of course we begin with checking that the date is in the future, otherwise we return "the time is already in the past!")

    And of course I will give you a small example for testing the function:

    $testTime = strtotime("next Thursday");
    echo timeLeft($testTime);

    $testTime = strtotime("+1 week 2 days 4 hours 2 seconds")
    echo timeLeft($testTime);


    Obs, you need to feed the function with a unix time stamp, which strtotime is great for...
    Here you get two dates/times to try it with.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (9)
      Link Indexing (6)
      Mailing List Management (8)
      Miscellaneous (10)
      Networking (6)
      News Publishing (6)
      OOP (8)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (5)
      Postcards (0)
      Randomizing (8)
      Redirection (8)
      Searching (6)
      Security (6)
      Site Navigation (7)
      User Authentication (10)
      WAP and WML (7)
      Web Fetching (0)
      Web Traffic Analysis (11)
      XML and PHP (0)

    New

    Hot