time.php

来自「Cake Framwork , Excellent」· PHP 代码 · 共 538 行 · 第 1/2 页

PHP
538
字号
 */	function toAtom($dateString, $userOffset = null) {		$date = $this->fromString($dateString, $userOffset);		$ret = date('Y-m-d\TH:i:s\Z', $date);		return $this->output($ret);	}/** * Formats date for RSS feeds * * @param string $dateString Datetime string or Unix timestamp * @param int $userOffset User's offset from GMT (in hours) * @return string Formatted date string */	function toRSS($dateString, $userOffset = null) {		$date = $this->fromString($dateString, $userOffset);		$ret = date("r", $date);		return $this->output($ret);	}/** * 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. * * Options: *  'format' => a fall back format if the relative time is longer than the duration specified by end *  'end' =>  The end of relative time telling *  'userOffset' => Users offset from GMT (in hours) * * 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 $dateString Datetime string or Unix timestamp * @param array $options Default format if timestamp is used in $dateString * @return string Relative time string. */	function timeAgoInWords($dateTime, $options = array()) {		$userOffset = null;		if (is_array($options) && isset($options['userOffset'])) {			$userOffset = $options['userOffset'];		}		$in_seconds = $this->fromString($dateTime, $userOffset);		$backwards = ($in_seconds > time());		$format = 'j/n/y';		$end = '+1 month';		$now = 	time();		if (is_array($options)) {			if (isset($options['format'])) {				$format = $options['format'];				unset($options['format']);			}			if (isset($options['end'])) {				$end = $options['end'];				unset($options['end']);			}		} else {			$format = $options;		}		if ($backwards) {			$future_time = $in_seconds;			$past_time = $now;		} else {			$future_time = $now;			$past_time = $in_seconds;		}		$diff = $future_time - $past_time;		// If more than a week, then take into account the length of months		if ($diff >= 604800) {			$current = array();			$date = array();						list($future['H'], $future['i'], $future['s'], $future['d'], $future['m'], $future['Y']) = explode('/', date('H/i/s/d/m/Y', $future_time));			list($past['H'], $past['i'], $past['s'], $past['d'], $past['m'], $past['Y']) = explode('/', date('H/i/s/d/m/Y', $past_time));			$years = $months = $weeks = $days = $hours = $minutes = $seconds = 0;			if ($future['Y'] == $past['Y'] && $future['m'] == $past['m']) {				$months = 0;				$years = 0;			} else {				if ($future['Y'] == $past['Y']) {					$months = $future['m'] - $past['m'];				} else {					$years = $future['Y'] - $past['Y'];					$months = $future['m'] + ((12 * $years) - $past['m']);										if ($months >= 12) {						$years = floor($months / 12);						$months = $months - ($years * 12);					}										if ($future['m'] < $past['m'] && $future['Y'] - $past['Y'] == 1) {						$years --;					}				}			}			if ($future['d'] >= $past['d']) {				$days = $future['d'] - $past['d'];			} else {				$days_in_past_month = date('t', $past_time);				$days_in_future_month = date('t', mktime(0, 0, 0, $future['m'] - 1, 1, $future['Y']));				if (!$backwards) {					$days = ($days_in_past_month - $past['d']) + $future['d'];				} else {					$days = ($days_in_future_month - $past['d']) + $future['d'];				}				if ($future['m'] != $past['m']) {					$months --;				}			}			if ($months == 0 && $years >= 1 && $diff < ($years * 31536000)){				$months = 11;				$years --;			}			if ($months >= 12) {				$years = $years + 1;				$months = $months - 12;			}			if ($days >= 7) {				$weeks = floor($days / 7);				$days = $days - ($weeks * 7);			}		} else {			$years = $months = $weeks = 0;			$days = floor($diff / 86400);			$diff = $diff - ($days * 86400);			$hours = floor($diff / 3600);			$diff = $diff - ($hours * 3600);			$minutes = floor($diff / 60);			$diff = $diff - ($minutes * 60);			$seconds = $diff;		}		$relative_date = '';		$diff = $future_time - $past_time;		if ($diff > abs($now - $this->fromString($end))) {			$relative_date = 'on ' . date($format, $in_seconds);		} else {			if ($years > 0) {				// years and months and days				$relative_date .= ($relative_date ? ', ' : '') . $years . ' year' . ($years > 1 ? 's' : '');				$relative_date .= $months > 0 ? ($relative_date ? ', ' : '') . $months . ' month' . ($months > 1 ? 's' : '') : '';				$relative_date .= $weeks > 0 ? ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : '';					$relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : '';			} elseif (abs($months) > 0) {				// months, weeks and days				$relative_date .= ($relative_date ? ', ' : '') . $months . ' month' . ($months > 1 ? 's' : '');				$relative_date .= $weeks > 0 ? ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : '';				$relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : '';			} elseif (abs($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 (abs($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 (abs($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 (abs($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' : '');			}			if (!$backwards) {				$relative_date .= ' ago';			}		}		return $this->output($relative_date);	}/** * Alias for timeAgoInWords * * @param mixed $dateTime Datetime string (strtotime-compatible) or Unix timestamp * @param mixed $options Default format string, if timestamp is used in $dateTime, or an array of options to be passed *						 on to timeAgoInWords(). * @return string Relative time string. * @see		TimeHelper::timeAgoInWords */	function relativeTime($dateTime, $options = array()) {		return $this->timeAgoInWords($dateTime, $options);	}/** * Returns true if specified datetime was within the interval specified, else false. * * @param mixed $timeInterval the numeric value with space then time type. Example of valid types: 6 hours, 2 days, 1 minute. * @param int $userOffset User's offset from GMT (in hours) * @param mixed $dateString the datestring or unix timestamp to compare * @return bool */	function wasWithinLast($timeInterval, $dateString, $userOffset = null) {		$tmp = r(' ', '', $timeInterval);		if (is_numeric($tmp)) {			$timeInterval = $tmp.' days';		}		$date = $this->fromString($dateString, $userOffset);		$interval = $this->fromString('-'.$timeInterval);		if ($date >= $interval && $date <= time()) {			return true;		}		return false;	}/** * Returns gmt, given either a UNIX timestamp or a valid strtotime() date string. * * @param string $dateString Datetime string * @return string Formatted date string */	function gmt($string = null) {		if ($string != null) {			$string = $this->fromString($string);		} else {			$string = time();		}		$string = $this->fromString($string);		$hour = intval(date("G", $string));		$minute = intval(date("i", $string));		$second = intval(date("s", $string));		$month = intval(date("n", $string));		$day = intval(date("j", $string));		$year = intval(date("Y", $string));		$return = gmmktime($hour, $minute, $second, $month, $day, $year);		return $return;	}/** * Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string. * * @param string $dateString Datetime string * @param boolean $invalid flag to ignore results of fromString == false * @param int $userOffset User's offset from GMT (in hours) * @return string Formatted date string */	function format($format = 'd-m-Y', $date, $invalid = false, $userOffset = null) {		$date = $this->fromString($date, $userOffset);		if ($date === false && $invalid !== false) {			return $invalid;		} 		return date($format, $date);	}}?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?