help time ago code

Started on the old forum in April 2012 · view the original in the archive · old links redirect here

2 replies · 571 views · started 14 years ago · 1 min read

3 participants
stratosphere Junior · 42 posts #1

i need time ago code like in facebook. Im using lavalair. I will insert it to shout. Thanks.. 🙁

arnage Lead · 811 posts #2

This one is nice, it has it all so you can modify it to your needs easy.

function time_ago( $datefrom , $dateto = -1 , $append = 'ago') 
 { 
    if( $datefrom <= 0 OR !ctype_digit($datefrom) ) 
    { 
       return false; 
    } 
     
    if($dateto === -1) 
    { 
       $dateto = time(); 
    } 
     
    if( !ctype_digit($dateto) ) 
    { 
       return false; 
    } 
     
    $difference = $dateto - $datefrom; 
     
    if($difference < 60) 
    { 
       $time_ago   = $difference . ' second' . ( $difference > 1 ? 's' : '' ).' '.$append; 
       #$interval = 's'; 
    } 
    else if( $difference < 60*60 ) 
    { 
       $ago_seconds   = $difference % 60; 
       $ago_seconds   = ( ( $ago_seconds AND $ago_seconds > 1 ) ? ' and '.$ago_seconds.' seconds' : ( $ago_seconds == 1 ? ' and '.$ago_seconds.' second' : '' ) ); 
       $ago_minutes   = floor( $difference / 60 ); 
       $ago_minutes   = $ago_minutes . ' minute' . ( $ago_minutes > 1 ? 's' : '' ); 
       $time_ago      = $ago_minutes.$ago_seconds.' '.$append; 
        
    } 
    else if ( $difference < 60*60*24 ) 
    { 
       $ago_minutes   = round( $difference / 60 ) % 60 ; 
       $ago_minutes   = ( ( $ago_minutes AND $ago_minutes > 1 ) ? ' and ' . $ago_minutes . ' minutes' : ( $ago_minutes == 1 ? ' and ' . $ago_minutes .' minute' : '' )); 
       $ago_hours      = floor( $difference / ( 60 * 60 ) ); 
       $ago_hours      = $ago_hours . ' hour'. ( $ago_hours > 1 ? 's' : '' ); 
       $time_ago      = $ago_hours.$ago_minutes.' '.$append; 
        
    } 
    else if ( $difference < 60*60*24*7 ) 
    { 
       $ago_hours      = round( $difference / 3600 ) % 24 ; 
       $ago_hours      = ( ( $ago_hours AND $ago_hours > 1 ) ? ' and ' . $ago_hours . ' hours' : ( $ago_hours == 1 ? ' and ' . $ago_hours . ' hour' : '' )); 
       $ago_days      = floor( $difference / ( 3600 * 24 ) ); 
       $ago_days      = $ago_days . ' day' . ($ago_days > 1 ? 's' : '' ); 
       $time_ago      = $ago_days.$ago_hours.' '.$append; 
        
    } 
    else if ( $difference < 60*60*24*30 ) 
    { 
       $ago_days      = round( $difference / ( 3600 * 24 ) ) % 7; 
       $ago_days      = ( ( $ago_days AND $ago_days > 1 ) ? ' and '.$ago_days.' days' : ( $ago_days == 1 ? ' and '.$ago_days.' day' : '' )); 
       $ago_weeks      = floor( $difference / ( 3600 * 24 * 7) ); 
       $ago_weeks      = $ago_weeks . ' week'. ($ago_weeks > 1 ? 's' : '' ); 
       $time_ago      = $ago_weeks.$ago_days.' '.$append; 
        
    } 
    else if ( $difference < 60*60*24*365 ) 
    { 
       $days_diff   = round( $difference / ( 60 * 60 * 24 ) ); 
       $ago_days   = $days_diff %  30 ; 
       $ago_weeks   = round( $ago_days / 7 ) ; 
       $ago_weeks   = ( ( $ago_weeks AND $ago_weeks > 1 ) ? ' and '.$ago_weeks.' weeks' : ( $ago_weeks == 1 ? ' and '.$ago_weeks.' week' : '' ) ); 
       $ago_months   = floor( $days_diff / 30 ); 
       $ago_months   = $ago_months .' month'. ( $ago_months > 1 ? 's' : '' ); 
       $time_ago   = $ago_months.$ago_weeks.' '.$append; 
        
    } 
    else if ( $difference >= 60*60*24*365 ) 
    { 
       $ago_months   = round( $difference / ( 60 * 60 * 24 * 30.5 ) ) % 12; 
       $ago_months   = ( ( $ago_months AND $ago_months > 1 ) ? ' and ' . $ago_months . ' months' : ( $ago_months == 1 ? ' and '.$ago_months.' month' : '' ) ); 
       $ago_years   = floor( $difference / ( 60 * 60 * 24 * 365 ) );#30 * 12 
       $ago_years   = $ago_years . ' year'. ($ago_years > 1 ? 's' : '' ) ; 
       $time_ago   = $ago_years.$ago_months.' '.$append; 
        
    } 
    return $time_ago; 
 }
<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
asifnayem Developer · 119 posts #3

I am using this:

//--------------
$LANG['secago'] = "Secs Ago";
$LANG['minago'] = "A minute Ago";
$LANG['minago2'] = "Minutes Ago";
$LANG['hrago'] = "An hour Ago";
$LANG['hrago2'] = "Hours Ago";
$LANG['tat'] = "Today at";
$LANG['yat'] = "Yesterday at";
//--------------

function timedate($time){
global $LANG;
$now = time();
$sodtime = mktime(0,0,0, date('m',$time), date('d',$time), date('Y',$time));
$sodnow = mktime(0,0,0, date('m',$now), date('d',$now), date('Y',$now));
if($sodtime==$sodnow){
$ms = $now-$time;
$hrs = floor($ms/60/60);
$mins = floor($ms/60);
 if($hrs > 0){
  if($hrs == 1){ return $LANG['hrago']; }
  elseif($hrs <= 22){ return $hrs." ".$LANG['hrago2']; }
  else{ return $LANG['tat'].' '.date('g:iA',$time); }
 }
 if($mins > 0){
   if($mins == 1){return $LANG['minago']; }
   else{ return "$mins ".$LANG['minago2']; }				
 }
 if($ms < 60){
 return "$ms ".$LANG['secago'];				
 }
return $LANG['tat'].' '.date('g:iA',$time);
}
elseif(($sodnow-$sodtime)<=86400){
return $LANG['yat'].' '.date('g:iA',$time);
}
elseif(($sodnow-$sodtime)<=432000){
return date('l g:iA', $time);
}
elseif(date('y',$now)==date('y',$time)){
return date('j, M g:iA',$time);
}
else{
return date('M j, Y g:iA',$time);
}
}
//--------------

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close