1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP Function to calculate time remaining

Discussion in 'PHP' started by pradeep, Oct 3, 2005.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    Many times it is required for us to calculate the time remaining and display it in a very understandable format like days,weeks,hours etc.
    Here is a function i wrote to do the same.

    PHP:
    //FUNCTION FOR TIME LEFT
     
    function time_left($integer)
     { 
     
         
    $seconds=$integer
     
         if (
    $seconds/60 >=1
     
         { 
     
         
    $minutes=floor($seconds/60); 
     
         if (
    $minutes/60 >= 1
     
         { 
    # Hours 
     
         
    $hours=floor($minutes/60); 
     
         if (
    $hours/24 >= 1
     
         { 
    #days 
     
         
    $days=floor($hours/24); 
     
         if (
    $days/>=1
     
         { 
    #weeks 
     
         
    $weeks=floor($days/7); 
     
         if (
    $weeks>=2$return="$weeks Weeks"
     
         else 
    $return="$weeks Week"
     
         } 
    #end of weeks 
     
         
    $days=$days-(floor($days/7))*7
     
         if (
    $weeks>=&& $days >=1$return="$return, "
     
         if (
    $days >=2$return="$return $days days";
     
         if (
    $days ==1$return="$return $days day";
     
         } 
    #end of days
     
         
    $hours=$hours-(floor($hours/24))*24
     
         if (
    $days>=&& $hours >=1$return="$return, "
     
         if (
    $hours >=2$return="$return $hours hours";
     
         if (
    $hours ==1$return="$return $hours hour";
     
         } 
    #end of Hours
     
         
    $minutes=$minutes-(floor($minutes/60))*60
     
         if (
    $hours>=&& $minutes >=1$return="$return, "
     
         if (
    $minutes >=2$return="$return $minutes minutes";
     
         if (
    $minutes ==1$return="$return $minutes minute";
     
         } 
    #end of minutes 
     
         
    $seconds=$integer-(floor($integer/60))*60
     
         if (
    $minutes>=&& $seconds >=1$return="$return, "
     
         if (
    $seconds >=2$return="$return $seconds seconds";
     
         if (
    $seconds ==1$return="$return $seconds second";
     
         
    $return="$return."
     
         return 
    $return
     
     }
     
     
    /* Returns a string of the amount of time the integer (in seconds) refers
      to. 
      
      $timeleft=time_left(86400); 
      
      $timeleft='1 day'. 
      
      Will not return anything higher than weeks. False if $integer=0 or fails. 
      
      */ 
    Enjoy.
     
    1 person likes this.
  2. Abdallah

    Abdallah New Member

    Joined:
    Jan 7, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    thank you very much , really very nice
     
  3. lothar86

    lothar86 New Member

    Joined:
    Aug 8, 2012
    Messages:
    1
    Likes Received:
    1
    Trophy Points:
    0
    Thank you for this very useful piece of code. I had to make 2 small adjustments resulting from the php settings of my server.

    PHP:
    /**
     * FUNCTION FOR TIME LEFT
     * Returns a string of the amount of time the integer (in seconds) refers to.
     * $timeleft = time_left(86400);
     * $timeleft = '1 day';
     * Will not return anything higher than weeks. False if $integer=0 or fails. 
     * @author pradeep
     */
    function time_left($integer)

        
    $seconds=$integer

        if (
    $seconds/60 >=1
        { 
            
    $minutes=floor($seconds/60); 
            if (
    $minutes/60 >= 1
            { 
    # Hours 
                
    $hours=floor($minutes/60); 
                if (
    $hours/24 >= 1
                { 
    #days 
                    
    $days=floor($hours/24);                 
                    
    $weeks=floor($days/7);#moved here form "if ($days/7 >=1)"statement
                    
                    
    if ($days/>=1
                    { 
    #weeks 
                        
    if ($weeks>=2$return="$weeks Weeks"
                        else 
    $return="$weeks Week"

                    } 
    #end of weeks

                    
    $days=$days-(floor($days/7))*7;                 
                    
    $return="";#added
                    
    if ($weeks>=&& $days >=1$return="$return, "
                    if (
    $days >=2$return="$return $days days";
                    if (
    $days ==1$return="$return $days day";

                } 
    #end of days

                
    $hours=$hours-(floor($hours/24))*24
                if (
    $days>=&& $hours >=1$return="$return, "
                if (
    $hours >=2$return="$return $hours hours";
                if (
    $hours ==1$return="$return $hours hour";

            } 
    #end of Hours

            
    $minutes=$minutes-(floor($minutes/60))*60
            if (
    $hours>=&& $minutes >=1$return="$return, "
            if (
    $minutes >=2$return="$return $minutes minutes";
            if (
    $minutes ==1$return="$return $minutes minute";

        } 
    #end of minutes 

        
    $seconds=$integer-(floor($integer/60))*60
        if (
    $minutes>=&& $seconds >=1$return="$return, "
        if (
    $seconds >=2$return="$return $seconds seconds";
        if (
    $seconds ==1$return="$return $seconds second";

        
    $return="$return."
        return 
    $return;
    }
     
    1 person likes this.

Share This Page