This function is useful if you want to display how long ago / from now a content was published etc.
/** * * Returns the amount of the days, months etc ago/from now relative to current time * @param long $timestamp * @return string|null */ public function timeAgo( $timestamp ) { if ( !is_int( $timestamp ) ) return null; $days = date( 'L' )? 366 : 365; $suffix = ' ago'; $aMinute = 60; $aHour = $aMinute * 60; $aDay = $aHour * 24; $aWeek = $aDay * 7; $aYear = $aDay * $days; $aMonth = $aYear / 12; $units = array( $aYear, $aMonth, $aWeek, $aDay, $aHour, $aMinute, 1 ); $period = array( 'year', 'month', 'week', 'day', 'hour', 'minute', 'second' ); $now = time(); $diff = $now - $timestamp; if ( $diff < 0 ) $suffix = ' from now'; $diff = abs( $diff ); if ( $diff == 1 ) return '1 second' . $suffix; foreach( $units as $index => $unit ) { if ( $diff > $unit ) { $diff = round( $diff / $unit ); $period = $period[$index]; $text = ( $diff > 1 )? $period . 's' : $period; return $diff . ' ' . $text . $suffix; } } }