Return to Snippet

Revision: 48117
at June 24, 2011 04:06 by erraja_07


Initial Code
function time_ago( $datefrom )
	{
	 	$dateto = date('Y-m-d H:i:s');
	 //  echo $dateto."-";
	//   echo $datefrom;
	  
	   if($datefrom<=0) { return "A long time ago"; }
	   if($dateto==-1) { $dateto = time(); }
	  
	   $difference = $dateto - $datefrom;
	  
	   // Seconds
	   if($difference < 60)
	   {
		 // $time_ago   = $difference . ' second' . ( $difference > 1 ? 's' : '' ).' ago';
	  $time_ago ='Just now';
	   }
	  
	   // Minutes
	   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' : '' ).' ago';
			$time_ago      = $ago_minutes;
	   }
	  
	   // Hours
	   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.' ago';
	   }
	  
	   // Days
	   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.' ago';
	   }
	  
	   // Weeks
	   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.' ago';
	   }
	  
	   // Months
	   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.' ago';
	   }
	  
	   // Years
	   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.' ago';
	   }
	  
	   return $time_ago;
	}

Initial URL


Initial Description


Initial Title
PHP - Display Updated time

Initial Tags
php

Initial Language
PHP