2005-12-27 03:33:44 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Time Helper class file.
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2007-02-02 10:39:45 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
2008-01-01 22:18:17 +00:00
|
|
|
* Copyright 2005-2008, Cake Software Foundation, Inc.
|
2006-05-26 05:29:17 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
2005-12-27 03:33:44 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
2008-01-01 22:18:17 +00:00
|
|
|
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
|
2007-02-02 10:39:45 +00:00
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
2006-05-26 05:29:17 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.view.helpers
|
2007-02-02 10:39:45 +00:00
|
|
|
* @since CakePHP(tm) v 0.10.0.1076
|
2006-05-26 05:29:17 +00:00
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
2005-12-27 03:33:44 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Time Helper class for easy use of time data.
|
|
|
|
*
|
|
|
|
* Manipulation of time data.
|
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.view.helpers
|
2005-12-27 03:33:44 +00:00
|
|
|
*/
|
2006-10-27 21:40:34 +00:00
|
|
|
class TimeHelper extends AppHelper {
|
2005-12-27 03:33:44 +00:00
|
|
|
/**
|
|
|
|
* Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string.
|
|
|
|
*
|
|
|
|
* @param string $date_string Datetime string
|
|
|
|
* @return string Formatted date string
|
|
|
|
*/
|
2006-06-15 15:29:06 +00:00
|
|
|
function fromString($date_string) {
|
2006-07-12 04:25:00 +00:00
|
|
|
if (is_integer($date_string) || is_numeric($date_string)) {
|
|
|
|
return intval($date_string);
|
2006-06-15 15:29:06 +00:00
|
|
|
} else {
|
|
|
|
return strtotime($date_string);
|
|
|
|
}
|
|
|
|
}
|
2005-12-27 03:33:44 +00:00
|
|
|
/**
|
|
|
|
* Returns a nicely formatted date string for given Datetime string.
|
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* @param string $date_string Datetime string or Unix timestamp
|
|
|
|
* @return string Formatted date string
|
2005-12-27 03:33:44 +00:00
|
|
|
*/
|
2006-08-22 07:26:03 +00:00
|
|
|
function nice($date_string = null) {
|
2006-06-15 15:29:06 +00:00
|
|
|
if ($date_string != null) {
|
|
|
|
$date = $this->fromString($date_string);
|
|
|
|
} else {
|
|
|
|
$date = time();
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2006-06-15 15:29:06 +00:00
|
|
|
$ret = date("D, M jS Y, H:i", $date);
|
2006-08-22 07:26:03 +00:00
|
|
|
return $this->output($ret);
|
2006-06-15 15:29:06 +00:00
|
|
|
}
|
2005-12-27 03:33:44 +00:00
|
|
|
/**
|
|
|
|
* Returns a formatted descriptive date string for given datetime string.
|
|
|
|
*
|
|
|
|
* If the given date is today, the returned string could be "Today, 16:54".
|
|
|
|
* If the given date was yesterday, the returned string could be "Yesterday, 16:54".
|
|
|
|
* If $date_string's year is the current year, the returned string does not
|
|
|
|
* include mention of the year.
|
|
|
|
*
|
|
|
|
* @param string $date_string Datetime string or Unix timestamp
|
|
|
|
* @return string Described, relative date string
|
|
|
|
*/
|
2006-08-22 07:26:03 +00:00
|
|
|
function niceShort($date_string = null) {
|
2006-06-15 15:29:06 +00:00
|
|
|
$date = $date_string ? $this->fromString($date_string) : time();
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2006-06-15 15:29:06 +00:00
|
|
|
$y = $this->isThisYear($date) ? '' : ' Y';
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2006-06-15 15:29:06 +00:00
|
|
|
if ($this->isToday($date)) {
|
|
|
|
$ret = "Today, " . date("H:i", $date);
|
2007-06-20 06:15:35 +00:00
|
|
|
} elseif ($this->wasYesterday($date)) {
|
2006-06-15 15:29:06 +00:00
|
|
|
$ret = "Yesterday, " . date("H:i", $date);
|
|
|
|
} else {
|
|
|
|
$ret = date("M jS{$y}, H:i", $date);
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2006-08-22 07:26:03 +00:00
|
|
|
return $this->output($ret);
|
2006-06-15 15:29:06 +00:00
|
|
|
}
|
2005-12-27 03:33:44 +00:00
|
|
|
/**
|
|
|
|
* Returns a partial SQL string to search for all records between two dates.
|
|
|
|
*
|
|
|
|
* @param string $date_string Datetime string or Unix timestamp
|
|
|
|
* @param string $end Datetime string or Unix timestamp
|
|
|
|
* @param string $field_name Name of database field to compare with
|
|
|
|
* @return string Partial SQL string.
|
|
|
|
*/
|
2006-08-22 07:26:03 +00:00
|
|
|
function daysAsSql($begin, $end, $field_name) {
|
2006-06-15 15:29:06 +00:00
|
|
|
$begin = $this->fromString($begin);
|
|
|
|
$end = $this->fromString($end);
|
|
|
|
$begin = date('Y-m-d', $begin) . ' 00:00:00';
|
|
|
|
$end = date('Y-m-d', $end) . ' 23:59:59';
|
2005-12-27 03:33:44 +00:00
|
|
|
|
2006-06-15 15:29:06 +00:00
|
|
|
$ret ="($field_name >= '$begin') AND ($field_name <= '$end')";
|
2006-08-22 07:26:03 +00:00
|
|
|
return $this->output($ret);
|
2006-06-15 15:29:06 +00:00
|
|
|
}
|
2005-12-27 03:33:44 +00:00
|
|
|
/**
|
|
|
|
* Returns a partial SQL string to search for all records between two times
|
|
|
|
* occurring on the same day.
|
|
|
|
*
|
|
|
|
* @param string $date_string Datetime string or Unix timestamp
|
|
|
|
* @param string $field_name Name of database field to compare with
|
|
|
|
* @return string Partial SQL string.
|
|
|
|
*/
|
2006-08-22 07:26:03 +00:00
|
|
|
function dayAsSql($date_string, $field_name) {
|
2006-06-15 15:29:06 +00:00
|
|
|
$date = $this->fromString($date_string);
|
|
|
|
$ret = $this->daysAsSql($date_string, $date_string, $field_name);
|
2006-08-22 07:26:03 +00:00
|
|
|
return $this->output($ret);
|
2006-06-15 15:29:06 +00:00
|
|
|
}
|
2006-06-15 19:26:13 +00:00
|
|
|
/**
|
|
|
|
* Returns true if given datetime string is today.
|
|
|
|
*
|
|
|
|
* @param string $date_string Datetime string or Unix timestamp
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True if datetime string is today
|
2006-06-15 19:26:13 +00:00
|
|
|
*/
|
2006-08-22 07:26:03 +00:00
|
|
|
function isToday($date_string) {
|
2006-06-15 19:26:13 +00:00
|
|
|
$date = $this->fromString($date_string);
|
2007-12-24 02:16:20 +00:00
|
|
|
return date('Y-m-d', $date) == date('Y-m-d', time());
|
2006-06-15 19:26:13 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns true if given datetime string is within this week
|
|
|
|
* @param string $date_string
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True if datetime string is within current week
|
2006-06-15 19:26:13 +00:00
|
|
|
*/
|
|
|
|
function isThisWeek($date_string) {
|
2008-05-05 14:38:03 +00:00
|
|
|
$date = $this->fromString($date_string);
|
2006-06-15 19:26:13 +00:00
|
|
|
return date('W Y', $date) == date('W Y', time());
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns true if given datetime string is within this month
|
|
|
|
* @param string $date_string
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True if datetime string is within current month
|
2006-06-15 19:26:13 +00:00
|
|
|
*/
|
|
|
|
function isThisMonth($date_string) {
|
|
|
|
$date = $this->fromString($date_string);
|
|
|
|
return date('m Y',$date) == date('m Y', time());
|
|
|
|
}
|
2005-12-27 03:33:44 +00:00
|
|
|
/**
|
|
|
|
* Returns true if given datetime string is within current year.
|
|
|
|
*
|
|
|
|
* @param string $date_string Datetime string or Unix timestamp
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True if datetime string is within current year
|
2005-12-27 03:33:44 +00:00
|
|
|
*/
|
2006-08-22 07:26:03 +00:00
|
|
|
function isThisYear($date_string) {
|
2006-06-15 15:29:06 +00:00
|
|
|
$date = $this->fromString($date_string);
|
2007-12-24 02:16:20 +00:00
|
|
|
return date('Y', $date) == date('Y', time());
|
2006-06-15 15:29:06 +00:00
|
|
|
}
|
2005-12-27 03:33:44 +00:00
|
|
|
/**
|
|
|
|
* Returns true if given datetime string was yesterday.
|
|
|
|
*
|
|
|
|
* @param string $date_string Datetime string or Unix timestamp
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True if datetime string was yesterday
|
2005-12-27 03:33:44 +00:00
|
|
|
*/
|
2006-08-22 07:26:03 +00:00
|
|
|
function wasYesterday($date_string) {
|
2006-06-15 15:29:06 +00:00
|
|
|
$date = $this->fromString($date_string);
|
2007-12-24 02:16:20 +00:00
|
|
|
return date('Y-m-d', $date) == date('Y-m-d', strtotime('yesterday'));
|
2006-06-15 15:29:06 +00:00
|
|
|
}
|
2005-12-27 03:33:44 +00:00
|
|
|
/**
|
|
|
|
* Returns true if given datetime string is tomorrow.
|
|
|
|
*
|
|
|
|
* @param string $date_string Datetime string or Unix timestamp
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True if datetime string was yesterday
|
2005-12-27 03:33:44 +00:00
|
|
|
*/
|
2006-08-22 07:26:03 +00:00
|
|
|
function isTomorrow($date_string) {
|
2006-06-15 15:29:06 +00:00
|
|
|
$date = $this->fromString($date_string);
|
2007-12-24 02:16:20 +00:00
|
|
|
return date('Y-m-d', $date) == date('Y-m-d', strtotime('tomorrow'));
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns the quart
|
|
|
|
* @param string $date_string
|
|
|
|
* @param boolean $range if true returns a range in Y-m-d format
|
|
|
|
* @return boolean True if datetime string is within current week
|
|
|
|
*/
|
|
|
|
function toQuarter($date_string, $range = false) {
|
|
|
|
$time = $this->fromString($date_string);
|
|
|
|
$date = ceil(date('m', $time) / 3);
|
|
|
|
|
|
|
|
if ($range === true) {
|
|
|
|
$range = 'Y-m-d';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($range !== false) {
|
|
|
|
$year = date('Y', $time);
|
|
|
|
|
|
|
|
switch ($date) {
|
|
|
|
case 1:
|
|
|
|
$date = array($year.'-01-01', $year.'-03-31');
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
$date = array($year.'-04-01', $year.'-06-30');
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
$date = array($year.'-07-01', $year.'-09-30');
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
$date = array($year.'-10-01', $year.'-12-31');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->output($date);
|
2006-06-15 15:29:06 +00:00
|
|
|
}
|
2005-12-27 03:33:44 +00:00
|
|
|
/**
|
|
|
|
* Returns a UNIX timestamp from a textual datetime description. Wrapper for PHP function strtotime().
|
|
|
|
*
|
|
|
|
* @param string $date_string Datetime string to be represented as a Unix timestamp
|
2007-10-22 16:54:36 +00:00
|
|
|
* @return integer Unix timestamp
|
2005-12-27 03:33:44 +00:00
|
|
|
*/
|
2006-08-22 07:26:03 +00:00
|
|
|
function toUnix($date_string) {
|
2007-12-31 01:45:57 +00:00
|
|
|
$ret = $this->fromString($date_string);
|
2006-08-22 07:26:03 +00:00
|
|
|
return $this->output($ret);
|
2006-06-15 15:29:06 +00:00
|
|
|
}
|
2005-12-27 03:33:44 +00:00
|
|
|
/**
|
|
|
|
* Returns a date formatted for Atom RSS feeds.
|
|
|
|
*
|
|
|
|
* @param string $date_string Datetime string or Unix timestamp
|
|
|
|
* @return string Formatted date string
|
|
|
|
*/
|
2006-08-22 07:26:03 +00:00
|
|
|
function toAtom($date_string) {
|
2006-06-15 15:29:06 +00:00
|
|
|
$date = $this->fromString($date_string);
|
|
|
|
$ret = date('Y-m-d\TH:i:s\Z', $date);
|
2006-08-22 07:26:03 +00:00
|
|
|
return $this->output($ret);
|
2006-06-15 15:29:06 +00:00
|
|
|
}
|
2005-12-27 03:33:44 +00:00
|
|
|
/**
|
|
|
|
* Formats date for RSS feeds
|
|
|
|
*
|
|
|
|
* @param string $date_string Datetime string or Unix timestamp
|
|
|
|
* @return string Formatted date string
|
|
|
|
*/
|
2006-08-22 07:26:03 +00:00
|
|
|
function toRSS($date_string) {
|
2007-12-24 02:16:20 +00:00
|
|
|
$date = $this->fromString($date_string);
|
2006-06-15 15:29:06 +00:00
|
|
|
$ret = date("r", $date);
|
2006-08-22 07:26:03 +00:00
|
|
|
return $this->output($ret);
|
2006-06-15 15:29:06 +00:00
|
|
|
}
|
2005-12-27 03:33:44 +00:00
|
|
|
/**
|
|
|
|
* Returns either a relative date or a formatted date depending
|
|
|
|
* on the difference between the current time and given datetime.
|
2006-02-07 02:19:53 +00:00
|
|
|
* $datetime should be in a <i>strtotime</i>-parsable format, like MySQL's datetime datatype.
|
2005-12-27 03:33:44 +00:00
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* Relative dates look something like this:
|
|
|
|
* 3 weeks, 4 days ago
|
|
|
|
* 15 seconds ago
|
|
|
|
* Formatted dates look like this:
|
|
|
|
* on 02/18/2004
|
2005-12-27 03:33:44 +00:00
|
|
|
*
|
|
|
|
* 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
|
2007-12-31 01:45:57 +00:00
|
|
|
* @param array $options Default format if timestamp is used in $date_string
|
2005-12-27 03:33:44 +00:00
|
|
|
* @return string Relative time string.
|
|
|
|
*/
|
2008-02-28 05:06:37 +00:00
|
|
|
function timeAgoInWords($dateTime, $options = array()) {
|
2008-01-15 04:29:55 +00:00
|
|
|
$in_seconds = $this->fromString($dateTime);
|
2008-02-28 05:06:37 +00:00
|
|
|
$backwards = ($in_seconds > time());
|
2007-12-31 01:45:57 +00:00
|
|
|
|
|
|
|
$format = 'j/n/y';
|
2008-02-28 05:06:37 +00:00
|
|
|
$end = '+1 month';
|
|
|
|
$now = time();
|
2006-06-15 15:29:06 +00:00
|
|
|
|
2007-12-31 01:45:57 +00:00
|
|
|
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;
|
|
|
|
}
|
2006-06-15 15:29:06 +00:00
|
|
|
|
|
|
|
if ($backwards) {
|
2008-02-28 05:06:37 +00:00
|
|
|
$future_time = $in_seconds;
|
|
|
|
$past_time = $now;
|
2006-06-15 15:29:06 +00:00
|
|
|
} else {
|
2008-02-28 05:06:37 +00:00
|
|
|
$future_time = $now;
|
|
|
|
$past_time = $in_seconds;
|
2006-06-15 15:29:06 +00:00
|
|
|
}
|
2008-02-28 05:06:37 +00:00
|
|
|
$diff = $future_time - $past_time;
|
|
|
|
|
|
|
|
// If more than a week, then take into account the length of months
|
2008-05-05 14:38:03 +00:00
|
|
|
if ($diff >= 604800) {
|
2008-02-28 05:06:37 +00:00
|
|
|
$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));
|
2008-05-05 14:38:03 +00:00
|
|
|
|
2008-02-28 05:06:37 +00:00
|
|
|
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;
|
|
|
|
|
2008-05-05 14:38:03 +00:00
|
|
|
if ($future['Y'] == $past['Y'] && $future['m'] == $past['m']) {
|
2008-02-28 05:06:37 +00:00
|
|
|
$months = 0;
|
|
|
|
$years = 0;
|
|
|
|
} else {
|
2008-05-05 14:38:03 +00:00
|
|
|
if ($future['Y'] == $past['Y']) {
|
2008-02-28 05:06:37 +00:00
|
|
|
$months = $future['m'] - $past['m'];
|
|
|
|
} else {
|
|
|
|
$years = $future['Y'] - $past['Y'];
|
|
|
|
$months = $future['m'] + ((12 * $years) - $past['m']);
|
|
|
|
|
2008-05-05 14:38:03 +00:00
|
|
|
if ($months >= 12) {
|
2008-02-28 05:06:37 +00:00
|
|
|
$years = floor($months / 12);
|
|
|
|
$months = $months - ($years * 12);
|
|
|
|
}
|
|
|
|
|
2008-05-05 14:38:03 +00:00
|
|
|
if ($future['m'] < $past['m'] && $future['Y'] - $past['Y'] == 1) {
|
2008-02-28 05:06:37 +00:00
|
|
|
$years --;
|
|
|
|
}
|
2008-05-05 14:38:03 +00:00
|
|
|
}
|
2008-02-28 05:06:37 +00:00
|
|
|
}
|
|
|
|
|
2008-05-05 14:38:03 +00:00
|
|
|
if ($future['d'] >= $past['d']) {
|
2008-02-28 05:06:37 +00:00
|
|
|
$days = $future['d'] - $past['d'];
|
2008-05-05 14:38:03 +00:00
|
|
|
} else {
|
2008-02-28 05:06:37 +00:00
|
|
|
$days_in_past_month = date('t', $past_time);
|
|
|
|
$days_in_future_month = date('t', mktime(0, 0, 0, $future['m'] - 1, 1, $future['Y']));
|
2008-05-05 14:38:03 +00:00
|
|
|
|
|
|
|
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']) {
|
2008-02-28 05:06:37 +00:00
|
|
|
$months --;
|
|
|
|
}
|
|
|
|
}
|
2008-05-05 14:38:03 +00:00
|
|
|
|
|
|
|
if ($months == 0 && $years >= 1 && $diff < ($years * 31536000)){
|
2008-02-28 05:06:37 +00:00
|
|
|
$months = 11;
|
2008-05-05 14:38:03 +00:00
|
|
|
$years --;
|
2008-02-28 05:06:37 +00:00
|
|
|
}
|
2008-05-05 14:38:03 +00:00
|
|
|
|
|
|
|
if ($months >= 12) {
|
2008-02-28 05:06:37 +00:00
|
|
|
$years = $years + 1;
|
|
|
|
$months = $months - 12;
|
|
|
|
}
|
2008-05-05 14:38:03 +00:00
|
|
|
|
|
|
|
if ($days >= 7) {
|
2008-02-28 05:06:37 +00:00
|
|
|
$weeks = floor($days / 7);
|
|
|
|
$days = $days - ($weeks * 7);
|
|
|
|
}
|
|
|
|
} else {
|
2008-05-05 14:38:03 +00:00
|
|
|
$years = $months = $weeks = 0;
|
2008-02-28 05:06:37 +00:00
|
|
|
$days = floor($diff / 86400);
|
2008-05-05 14:38:03 +00:00
|
|
|
|
2008-02-28 05:06:37 +00:00
|
|
|
$diff = $diff - ($days * 86400);
|
2006-06-15 15:29:06 +00:00
|
|
|
|
2008-02-28 05:06:37 +00:00
|
|
|
$hours = floor($diff / 3600);
|
|
|
|
$diff = $diff - ($hours * 3600);
|
2006-06-15 15:29:06 +00:00
|
|
|
|
2008-02-28 05:06:37 +00:00
|
|
|
$minutes = floor($diff / 60);
|
|
|
|
$diff = $diff - ($minutes * 60);
|
|
|
|
$seconds = $diff;
|
2008-05-05 14:38:03 +00:00
|
|
|
}
|
2007-12-31 01:45:57 +00:00
|
|
|
$relative_date = '';
|
2008-02-28 05:06:37 +00:00
|
|
|
$diff = $future_time - $past_time;
|
2007-12-31 01:45:57 +00:00
|
|
|
|
2008-02-28 05:06:37 +00:00
|
|
|
if ($diff > abs($now - $this->fromString($end))) {
|
2006-06-15 15:29:06 +00:00
|
|
|
$relative_date = 'on ' . date($format, $in_seconds);
|
|
|
|
} else {
|
2008-02-28 05:06:37 +00:00
|
|
|
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' : '') : '';
|
2008-05-05 14:38:03 +00:00
|
|
|
$relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : '';
|
2008-02-28 05:06:37 +00:00
|
|
|
} elseif (abs($months) > 0) {
|
2007-12-31 01:45:57 +00:00
|
|
|
// 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) {
|
2006-06-15 15:29:06 +00:00
|
|
|
// weeks and days
|
|
|
|
$relative_date .= ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : '');
|
|
|
|
$relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : '';
|
2007-12-31 01:45:57 +00:00
|
|
|
} elseif (abs($days) > 0) {
|
2006-06-15 15:29:06 +00:00
|
|
|
// days and hours
|
|
|
|
$relative_date .= ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '');
|
|
|
|
$relative_date .= $hours > 0 ? ($relative_date ? ', ' : '') . $hours . ' hour' . ($hours > 1 ? 's' : '') : '';
|
2007-12-31 01:45:57 +00:00
|
|
|
} elseif (abs($hours) > 0) {
|
2006-06-15 15:29:06 +00:00
|
|
|
// hours and minutes
|
|
|
|
$relative_date .= ($relative_date ? ', ' : '') . $hours . ' hour' . ($hours > 1 ? 's' : '');
|
|
|
|
$relative_date .= $minutes > 0 ? ($relative_date ? ', ' : '') . $minutes . ' minute' . ($minutes > 1 ? 's' : '') : '';
|
2007-12-31 01:45:57 +00:00
|
|
|
} elseif (abs($minutes) > 0) {
|
2006-06-15 15:29:06 +00:00
|
|
|
// minutes only
|
|
|
|
$relative_date .= ($relative_date ? ', ' : '') . $minutes . ' minute' . ($minutes > 1 ? 's' : '');
|
|
|
|
} else {
|
|
|
|
// seconds only
|
|
|
|
$relative_date .= ($relative_date ? ', ' : '') . $seconds . ' second' . ($seconds != 1 ? 's' : '');
|
|
|
|
}
|
|
|
|
|
2007-12-31 01:45:57 +00:00
|
|
|
if (!$backwards) {
|
|
|
|
$relative_date .= ' ago';
|
|
|
|
}
|
2006-06-15 15:29:06 +00:00
|
|
|
}
|
2007-12-31 01:45:57 +00:00
|
|
|
return $this->output($relative_date);
|
2006-06-15 15:29:06 +00:00
|
|
|
}
|
2005-12-27 03:33:44 +00:00
|
|
|
/**
|
2008-02-28 05:06:37 +00:00
|
|
|
* Alias for timeAgoInWords
|
2008-01-15 04:29:55 +00:00
|
|
|
*
|
|
|
|
* @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().
|
2005-12-27 03:33:44 +00:00
|
|
|
* @return string Relative time string.
|
2008-01-15 04:29:55 +00:00
|
|
|
* @see TimeHelper::timeAgoInWords
|
2005-12-27 03:33:44 +00:00
|
|
|
*/
|
2008-02-28 05:06:37 +00:00
|
|
|
function relativeTime($dateTime, $options = array()) {
|
|
|
|
return $this->timeAgoInWords($dateTime, $options);
|
2006-06-15 15:29:06 +00:00
|
|
|
}
|
2005-12-27 03:33:44 +00:00
|
|
|
/**
|
|
|
|
* Returns true if specified datetime was within the interval specified, else false.
|
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* @param mixed $timeInterval the numeric value with space then time type. Example of valid types: 6 hours, 2 days, 1 minute.
|
2006-02-07 02:19:53 +00:00
|
|
|
* @param mixed $date_string the datestring or unix timestamp to compare
|
2007-10-22 05:52:20 +00:00
|
|
|
* @return bool
|
2005-12-27 03:33:44 +00:00
|
|
|
*/
|
2006-08-22 07:26:03 +00:00
|
|
|
function wasWithinLast($timeInterval, $date_string) {
|
2008-05-05 14:38:03 +00:00
|
|
|
$tmp = r(' ', '', $timeInterval);
|
2008-05-05 15:04:02 +00:00
|
|
|
if (is_numeric($tmp)) {
|
2008-05-05 14:38:03 +00:00
|
|
|
$timeInterval = $tmp.' days';
|
2006-06-15 15:29:06 +00:00
|
|
|
}
|
|
|
|
|
2008-05-05 14:38:03 +00:00
|
|
|
$date = $this->fromString($date_string);
|
|
|
|
$interval = $this->fromString('-'.$timeInterval);
|
|
|
|
|
|
|
|
if ($date >= $interval && $date <= time()) {
|
|
|
|
return true;
|
2006-06-15 15:29:06 +00:00
|
|
|
}
|
|
|
|
|
2008-05-05 14:38:03 +00:00
|
|
|
return false;
|
2006-06-15 15:29:06 +00:00
|
|
|
}
|
2007-12-24 02:16:20 +00:00
|
|
|
/**
|
|
|
|
* Returns gmt, given either a UNIX timestamp or a valid strtotime() date string.
|
|
|
|
*
|
|
|
|
* @param string $date_string Datetime string
|
|
|
|
* @return string Formatted date string
|
|
|
|
*/
|
2006-08-22 07:26:03 +00:00
|
|
|
function gmt($string = null) {
|
2006-08-03 18:50:06 +00:00
|
|
|
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;
|
|
|
|
}
|
2007-12-24 02:16:20 +00:00
|
|
|
/**
|
|
|
|
* Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string.
|
|
|
|
*
|
|
|
|
* @param string $date_string Datetime string
|
|
|
|
* @return string Formatted date string
|
|
|
|
*/
|
2006-10-13 07:41:10 +00:00
|
|
|
function format($format = 'd-m-Y', $date) {
|
|
|
|
return date($format, $this->fromString($date));
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-06-15 15:29:06 +00:00
|
|
|
|
2005-08-21 06:49:02 +00:00
|
|
|
?>
|