/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function time_ago( $datefrom ) { // echo $dateto."-"; // echo $datefrom; if($datefrom<=0) { return "A long time ago"; } $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 = $ago_minutes . ' minute' . ( $ago_minutes > 1 ? 's' : '' ).' ago'; $time_ago = $ago_minutes; } // Hours else if ( $difference < 60*60*24 ) { $ago_minutes = ( ( $ago_minutes AND $ago_minutes > 1 ) ? ' and ' . $ago_minutes . ' minutes' : ( $ago_minutes == 1 ? ' and ' . $ago_minutes .' minute' : '' )); $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 = ( ( $ago_hours AND $ago_hours > 1 ) ? ' and ' . $ago_hours . ' hours' : ( $ago_hours == 1 ? ' and ' . $ago_hours . ' hour' : '' )); $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 = ( ( $ago_days AND $ago_days > 1 ) ? ' and '.$ago_days.' days' : ( $ago_days == 1 ? ' and '.$ago_days.' day' : '' )); $ago_weeks = $ago_weeks . ' week'. ($ago_weeks > 1 ? 's' : '' ); $time_ago = $ago_weeks.$ago_days.' ago'; } // Months else if ( $difference < 60*60*24*365 ) { $ago_days = $days_diff % 30 ; $ago_weeks = ( ( $ago_weeks AND $ago_weeks > 1 ) ? ' and '.$ago_weeks.' weeks' : ( $ago_weeks == 1 ? ' and '.$ago_weeks.' week' : '' ) ); $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 = ( ( $ago_months AND $ago_months > 1 ) ? ' and ' . $ago_months . ' months' : ( $ago_months == 1 ? ' and '.$ago_months.' month' : '' ) ); $ago_years = $ago_years . ' year'. ($ago_years > 1 ? 's' : '' ) ; $time_ago = $ago_years.$ago_months.' ago'; } return $time_ago; }