All pastes #775433 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-11-15 16:27 UTC
rendered paste body
 * Formats date for RSS feeds
 *
 * @param string $date_string Datetime string or Unix timestamp
 * @param  boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT.
 * @return string Formatted date string
 * @access public
 */
	function toRSS($date_string, $return = false) {
		$date = $this->fromString($date_string);
		$ret = date("r", $date);
		return $this->output($ret, $return);
	}
/**
 * Returns either a relative date or a formatted date depending
 * on the difference between the current time and given datetime.
 * $datetime should be in a <i>strtotime</i>-parsable format, like MySQL's datetime datatype.
 *
 * Relative dates look something like this:
 *	3 weeks, 4 days ago
 *	15 seconds ago
 * Formatted dates look like this:
 *	on 02/18/2004
 *
 * The returned string includes 'ago' or 'on' and assumes you'll properly add a word
 * like 'Posted ' before the function output.
 *
 * @param string $date_string Datetime string or Unix timestamp
 * @param string $format Default format if timestamp is used in $date_string
 * @param string $backwards False if $date_string is in the past, true if in the future
 * @param  boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT.
 * @return string Relative time string.
 * @access public
 */
	function timeAgoInWords($datetime_string, $format = 'j/n/y', $backwards = false, $return = false) {
		$datetime = $this->fromString($datetime_string);

		$in_seconds = $datetime;
		if ($backwards) {
			$diff = $in_seconds - time();
		} else {
			$diff = time() - $in_seconds;
		}

		$months = floor($diff / 2419200);
		$diff -= $months * 2419200;
		$weeks = floor($diff / 604800);
		$diff -= $weeks * 604800;
		$days = floor($diff / 86400);
		$diff -= $days * 86400;
		$hours = floor($diff / 3600);
		$diff -= $hours * 3600;
		$minutes = floor($diff / 60);
		$diff -= $minutes * 60;
		$seconds = $diff;

		if ($months > 0) {
			// over a month old, just show date (mm/dd/yyyy format)
			$relative_date = 'on ' . date($format, $in_seconds);
			$old = true;
		} else {
			$relative_date = '';
			$old = false;

			if ($weeks > 0) {
				// weeks and days
				$relative_date .= ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : '');
				$relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : '';
			} elseif ($days > 0) {
				// days and hours
				$relative_date .= ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '');
				$relative_date .= $hours > 0 ? ($relative_date ? ', ' : '') . $hours . ' hour' . ($hours > 1 ? 's' : '') : '';
			} elseif ($hours > 0) {
				// hours and minutes
				$relative_date .= ($relative_date ? ', ' : '') . $hours . ' hour' . ($hours > 1 ? 's' : '');
				$relative_date .= $minutes > 0 ? ($relative_date ? ', ' : '') . $minutes . ' minute' . ($minutes > 1 ? 's' : '') : '';
			} elseif ($minutes > 0) {
				// minutes only
				$relative_date .= ($relative_date ? ', ' : '') . $minutes . ' minute' . ($minutes > 1 ? 's' : '');
			} else {
				// seconds only
				$relative_date .= ($relative_date ? ', ' : '') . $seconds . ' second' . ($seconds != 1 ? 's' : '');
			}
		}

		$ret = $relative_date;

		// show relative date and add proper verbiage
		if (!$backwards && !$old) {
			$ret .= ' ago';
		}
		return $this->output($ret, $return);
	}