define('INT_SECOND', 1); define('INT_MINUTE', 60); define('INT_HOUR', 3600); define('INT_DAY', 86400); define('INT_WEEK', 604800); function get_formatted_timediff($then, $now = false) { $now = (!$now) ? time() : $now; $timediff = ($now - $then); $weeks = (int) intval($timediff / INT_WEEK); $timediff = (int) intval($timediff - (INT_WEEK * $weeks)); $days = (int) intval($timediff / INT_DAY); $timediff = (int) intval($timediff - (INT_DAY * $days)); $hours = (int) intval($timediff / INT_HOUR); $timediff = (int) intval($timediff - (INT_HOUR * $hours)); $mins = (int) intval($timediff / INT_MINUTE); $timediff = (int) intval($timediff - (INT_MINUTE * $mins)); $sec = (int) intval($timediff / INT_SECOND); $timediff = (int) intval($timediff - ($sec * INT_SECOND)); $str = ''; if ( $weeks ) { $str .= intval($weeks); $str .= ($weeks > 1) ? ' weeks' : ' week'; } if ( $days ) { $str .= ($str) ? ', ' : ''; $str .= intval($days); $str .= ($days > 1) ? ' days' : ' day'; } if ( $hours ) { $str .= ($str) ? ', ' : ''; $str .= intval($hours); $str .= ($hours > 1) ? ' hours' : ' hour'; } if ( $mins ) { $str .= ($str) ? ', ' : ''; $str .= intval($mins); $str .= ($mins > 1) ? ' minutes' : ' minute'; } if ( $sec ) { $str .= ($str) ? ', ' : ''; $str .= intval($sec); $str .= ($sec > 1) ? ' seconds' : ' second'; } if ( !$weeks && !$days && !$hours && !$mins && !$sec ) { $str .= '0 seconds ago'; } else { $str .= ' ago'; } return $str; }