Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Compute time difference / time_since

    I found this time_since function via a google search, and it works well. But I'm unsure on how to have it includes the minutes. Currently it shows X day, X hours,

    PHP Code:
    function time_since($original) {
        
    // array of time period chunks
        
    $chunks = array(
            array(
    60 60 24 365 'year'),
            array(
    60 60 24 30 'month'),
            array(
    60 60 24 7'week'),
            array(
    60 60 24 'day'),
            array(
    60 60 'hour'),
            array(
    60 'minute'),
        );
        
        
    $today time(); /* Current unix time  */
        
    $since $today $original;
        
        
    // $j saves performing the count function each time around the loop
        
    for ($i 0$j count($chunks); $i $j$i++) {
            
            
    $seconds $chunks[$i][0];
            
    $name $chunks[$i][1];
            
            
    // finding the biggest chunk (if the chunk fits, break)
            
    if (($count floor($since $seconds)) != 0) {
                
    // DEBUG print "<!-- It's $name -->\n";
                
    break;
            }
        }
        
        
    $print = ($count == 1) ? '1 '.$name "$count {$name}s";
        
        if (
    $i $j) {
            
    // now getting the second item
            
    $seconds2 $chunks[$i 1][0];
            
    $name2 $chunks[$i 1][1];
            
            
    // add second item if it's greater than 0
            
    if (($count2 floor(($since - ($seconds $count)) / $seconds2)) != 0) {
                
    $print .= ($count2 == 1) ? ', 1 '.$name2 ", $count2 {$name2}s";
            }
        }
        return 
    $print;
    }
    $time '1211373000';
    echo 
    time_since($time); 

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Nevermind, I'm learning

    I simply added this to the code:

    PHP Code:
     // now get the third item
            
    $minutes3 $chunks[$i 2][0];
            
    $name3 $chunks[$i 2][1];
            
            
    // add third item if it's greater than 0
            
    if (($count3 floor(($since - ($seconds $count)) / $minutes3)) != 0) {
                
    $print .= ($count3 == 1) ? ', 1 '.$name3 ", $count3 {$name3}s";
            } 

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Good good, gotta start somewhere, learning the ideas behind how things work is a great start! I always find that dates/times are a pain no matter what language you are using (if you are using manual stuff).
    Once you get proficient with the manual control of the time - PHP has IMO the greatest date/time handling of any language I've used. You may want to keep this link in mind. You will be surprised what php can do for you automatically!
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •