7 Replies - 1901 Views - Last Post: 19 February 2011 - 12:49 AM

#1 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1953
  • View blog
  • Posts: 20,377
  • Joined: 17-March 01

Years, Days, Hours, Minutes Ago

Posted 02 March 2005 - 12:07 PM

Description: Call using time_since(aUNIXTimeStamp);

Returns a string. The time_since() function returns a nicely formatted Days Ago type date. Just pass a UNIX timestamp in and you'll get the string back out.
/* Works out the time since the entry post, takes a an argument in unix time (seconds) */
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 + 1 < $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;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Years, Days, Hours, Minutes Ago

#2 Glasseater  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 15-June 07

Re: Years, Days, Hours, Minutes Ago

Posted 03 February 2008 - 04:32 PM

oooo... I like :D Thanks for sharing this one.
Was This Post Helpful? 0
  • +
  • -

#3 ludjer  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 186
  • Joined: 31-October 08

Re: Years, Days, Hours, Minutes Ago

Posted 02 December 2008 - 03:26 AM

looks good gona give it a try thanks
Was This Post Helpful? 0
  • +
  • -

#4 RayRayAngel  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 15-December 08

Re: Years, Days, Hours, Minutes Ago

Posted 30 December 2008 - 02:55 AM

What if I want to go the other way? Instead of counting time since.... counting down to the original value?
Was This Post Helpful? 0
  • +
  • -

#5 arti306  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 17-June 10

Re: Years, Days, Hours, Minutes Ago

Posted 17 June 2010 - 09:24 AM

I have the same question as RayRay Angel.Please can anyone tell me reverse of the timesince.Like now it's giving us 4 days ago.I need like 2 days left.or 1 hr left ....something like that.
Was This Post Helpful? 0
  • +
  • -

#6 ssafiullah_h  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 16-November 10

Re: Years, Days, Hours, Minutes Ago

Posted 16 November 2010 - 02:44 AM

useful code snippet
Was This Post Helpful? 0
  • +
  • -

#7 jegsay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 12-February 11

Re: Years, Days, Hours, Minutes Ago

Posted 12 February 2011 - 10:18 PM

looks cool n i ganna giv it a shot
Was This Post Helpful? 0
  • +
  • -

#8 iffii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-February 11

Re: Years, Days, Hours, Minutes Ago

Posted 19 February 2011 - 12:49 AM

nice work.....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1