/** * Returns time ago in words of the given date. * * @param string $original * @param boolean $detailed * * @return string */ public static function agoInWords($original, $detailed = false) { // Check what kind of format we're dealing with, timestamp or datetime // and convert it to a timestamp if it is in datetime form. if (!is_numeric($original)) { $original = static::toUnix($original); } $now = time(); // Get the time right now... // Time chunks... $chunks = array(array(60 * 60 * 24 * 365, 'year', 'years'), array(60 * 60 * 24 * 30, 'month', 'months'), array(60 * 60 * 24 * 7, 'week', 'weeks'), array(60 * 60 * 24, 'day', 'days'), array(60 * 60, 'hour', 'hours'), array(60, 'minute', 'minutes'), array(1, 'second', 'seconds')); // Get the difference $difference = $now > $original ? $now - $original : $original - $now; // Loop around, get the time from for ($i = 0, $c = count($chunks); $i < $c; $i++) { $seconds = $chunks[$i][0]; $name = $chunks[$i][1]; $names = $chunks[$i][2]; if (0 != ($count = floor($difference / $seconds))) { break; } } // Format the time from $from = Language::current()->translate("time.x_{$name}", array($count)); // Get the detailed time from if the detail variable is true if ($detailed && $i + 1 < $c) { $seconds2 = $chunks[$i + 1][0]; $name2 = $chunks[$i + 1][1]; $names2 = $chunks[$i + 1][2]; if (0 != ($count2 = floor(($difference - $seconds * $count) / $seconds2))) { $from = Language::current()->translate('time.x_and_x', $from, Language::current()->translate("time.x_{$name2}", array($count2))); } } // Return the time from return $from; }