2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Time Helper class file.
|
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.View.Helper
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 0.10.0.1076
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-12-04 18:10:24 +00:00
|
|
|
App::uses('AppHelper', 'View/Helper');
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Time Helper class for easy use of time data.
|
|
|
|
*
|
|
|
|
* Manipulation of time data.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.View.Helper
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1470/Time
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class TimeHelper extends AppHelper {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-01-06 03:06:52 +00:00
|
|
|
/**
|
|
|
|
* The format to use when formatting a time using `TimeHelper::nice()`
|
|
|
|
*
|
|
|
|
* The format should use the locale strings as defined in the PHP docs under
|
|
|
|
* `strftime` (http://php.net/manual/en/function.strftime.php)
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @see TimeHelper::format()
|
|
|
|
*/
|
|
|
|
public $niceFormat = '%a, %b %eS %Y, %H:%M';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param View $View the view object the helper is attached to.
|
|
|
|
* @param array $settings Settings array Settings array
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct(View $View, $settings = array()) {
|
|
|
|
if (isset($settings['niceFormat'])) {
|
|
|
|
$this->niceFormat = $settings['niceFormat'];
|
|
|
|
}
|
|
|
|
parent::__construct($View, $settings);
|
|
|
|
}
|
|
|
|
|
2010-01-14 18:41:57 +00:00
|
|
|
/**
|
|
|
|
* Converts a string representing the format for the function strftime and returns a
|
|
|
|
* windows safe and i18n aware format.
|
|
|
|
*
|
2011-02-21 19:54:21 +00:00
|
|
|
* @param string $format Format with specifiers for strftime function.
|
2010-01-25 22:59:05 +00:00
|
|
|
* Accepts the special specifier %S which mimics th modifier S for date()
|
2010-01-14 18:41:57 +00:00
|
|
|
* @param string UNIX timestamp
|
|
|
|
* @return string windows safe and date() function compatible format for strftime
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function convertSpecifiers($format, $time = null) {
|
2010-01-14 18:41:57 +00:00
|
|
|
if (!$time) {
|
|
|
|
$time = time();
|
|
|
|
}
|
|
|
|
$this->__time = $time;
|
2010-01-14 20:08:48 +00:00
|
|
|
return preg_replace_callback('/\%(\w+)/', array($this, '__translateSpecifier'), $format);
|
2010-01-14 18:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Auxiliary function to translate a matched specifier element from a regular expresion into
|
|
|
|
* a windows safe and i18n aware specifier
|
|
|
|
*
|
|
|
|
* @param array $specifier match from regular expression
|
|
|
|
* @return string converted element
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access private
|
2010-01-14 18:41:57 +00:00
|
|
|
*/
|
2011-02-21 19:54:21 +00:00
|
|
|
private function __translateSpecifier($specifier) {
|
2010-01-14 18:41:57 +00:00
|
|
|
switch ($specifier[1]) {
|
|
|
|
case 'a':
|
2011-03-21 05:27:37 +00:00
|
|
|
$abday = __dc('cake', 'abday', 5);
|
2010-01-14 18:41:57 +00:00
|
|
|
if (is_array($abday)) {
|
2010-01-14 20:08:48 +00:00
|
|
|
return $abday[date('w', $this->__time)];
|
2010-01-14 18:41:57 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'A':
|
2011-03-21 05:27:37 +00:00
|
|
|
$day = __dc('cake', 'day', 5);
|
2010-01-14 18:41:57 +00:00
|
|
|
if (is_array($day)) {
|
2010-01-14 20:08:48 +00:00
|
|
|
return $day[date('w', $this->__time)];
|
2010-01-14 18:41:57 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'c':
|
2011-03-21 05:27:37 +00:00
|
|
|
$format = __dc('cake', 'd_t_fmt', 5);
|
2010-01-14 18:41:57 +00:00
|
|
|
if ($format != 'd_t_fmt') {
|
2010-01-14 20:08:48 +00:00
|
|
|
return $this->convertSpecifiers($format, $this->__time);
|
2010-01-14 18:41:57 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
return sprintf("%02d", date('Y', $this->__time) / 100);
|
|
|
|
case 'D':
|
|
|
|
return '%m/%d/%y';
|
2011-02-06 17:51:28 +00:00
|
|
|
case 'e':
|
|
|
|
if (DS === '/') {
|
|
|
|
return '%e';
|
|
|
|
}
|
|
|
|
$day = date('j', $this->__time);
|
|
|
|
if ($day < 10) {
|
|
|
|
$day = ' ' . $day;
|
|
|
|
}
|
|
|
|
return $day;
|
2010-01-14 18:41:57 +00:00
|
|
|
case 'eS' :
|
2010-01-14 20:08:48 +00:00
|
|
|
return date('jS', $this->__time);
|
2010-01-14 18:41:57 +00:00
|
|
|
case 'b':
|
|
|
|
case 'h':
|
2011-03-21 05:27:37 +00:00
|
|
|
$months = __dc('cake', 'abmon', 5);
|
2010-01-14 18:41:57 +00:00
|
|
|
if (is_array($months)) {
|
2010-01-14 20:08:48 +00:00
|
|
|
return $months[date('n', $this->__time) -1];
|
2010-01-14 18:41:57 +00:00
|
|
|
}
|
|
|
|
return '%b';
|
|
|
|
case 'B':
|
2011-03-21 05:27:37 +00:00
|
|
|
$months = __dc('cake', 'mon', 5);
|
2010-01-14 18:41:57 +00:00
|
|
|
if (is_array($months)) {
|
2010-01-14 20:08:48 +00:00
|
|
|
return $months[date('n', $this->__time) -1];
|
2010-01-14 18:41:57 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
return "\n";
|
|
|
|
case 'p':
|
|
|
|
case 'P':
|
|
|
|
$default = array('am' => 0, 'pm' => 1);
|
|
|
|
$meridiem = $default[date('a',$this->__time)];
|
2011-03-21 05:27:37 +00:00
|
|
|
$format = __dc('cake', 'am_pm', 5);
|
2010-01-14 18:41:57 +00:00
|
|
|
if (is_array($format)) {
|
|
|
|
$meridiem = $format[$meridiem];
|
|
|
|
return ($specifier[1] == 'P') ? strtolower($meridiem) : strtoupper($meridiem);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'r':
|
2011-03-21 05:27:37 +00:00
|
|
|
$complete = __dc('cake', 't_fmt_ampm', 5);
|
2010-01-14 18:41:57 +00:00
|
|
|
if ($complete != 't_fmt_ampm') {
|
2010-01-14 20:08:48 +00:00
|
|
|
return str_replace('%p',$this->__translateSpecifier(array('%p', 'p')),$complete);
|
2010-01-14 18:41:57 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
return date('H:i', $this->__time);
|
|
|
|
case 't':
|
|
|
|
return "\t";
|
|
|
|
case 'T':
|
|
|
|
return '%H:%M:%S';
|
|
|
|
case 'u':
|
|
|
|
return ($weekDay = date('w', $this->__time)) ? $weekDay : 7;
|
|
|
|
case 'x':
|
2011-03-21 05:27:37 +00:00
|
|
|
$format = __dc('cake', 'd_fmt', 5);
|
2010-01-14 18:41:57 +00:00
|
|
|
if ($format != 'd_fmt') {
|
2010-01-14 20:08:48 +00:00
|
|
|
return $this->convertSpecifiers($format, $this->__time);
|
2010-01-14 18:41:57 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'X':
|
2011-03-21 05:27:37 +00:00
|
|
|
$format = __dc('cake', 't_fmt', 5);
|
2010-01-14 18:41:57 +00:00
|
|
|
if ($format != 't_fmt') {
|
2010-01-14 20:08:48 +00:00
|
|
|
return $this->convertSpecifiers($format, $this->__time);
|
2010-01-14 18:41:57 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return $specifier[0];
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Converts given time (in server's time zone) to user's local time, given his/her offset from GMT.
|
|
|
|
*
|
2008-10-23 00:10:44 +00:00
|
|
|
* @param string $serverTime UNIX timestamp
|
|
|
|
* @param int $userOffset User's offset from GMT (in hours)
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string UNIX timestamp
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function convert($serverTime, $userOffset) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$serverOffset = $this->serverOffset();
|
|
|
|
$gmtTime = $serverTime - $serverOffset;
|
|
|
|
$userTime = $gmtTime + $userOffset * (60*60);
|
|
|
|
return $userTime;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns server's offset from GMT in seconds.
|
|
|
|
*
|
|
|
|
* @return int Offset
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function serverOffset() {
|
2008-10-08 17:19:07 +00:00
|
|
|
return date('Z', time());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string.
|
|
|
|
*
|
|
|
|
* @param string $dateString Datetime string
|
|
|
|
* @param int $userOffset User's offset from GMT (in hours)
|
2008-10-08 17:19:07 +00:00
|
|
|
* @return string Parsed timestamp
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1471/Formatting
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function fromString($dateString, $userOffset = null) {
|
2008-10-08 17:19:07 +00:00
|
|
|
if (empty($dateString)) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-01-14 18:41:57 +00:00
|
|
|
if (is_integer($dateString) || is_numeric($dateString)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$date = intval($dateString);
|
|
|
|
} else {
|
|
|
|
$date = strtotime($dateString);
|
|
|
|
}
|
|
|
|
if ($userOffset !== null) {
|
|
|
|
return $this->convert($date, $userOffset);
|
|
|
|
}
|
2010-01-15 22:10:49 +00:00
|
|
|
if ($date === -1) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
return $date;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a nicely formatted date string for given Datetime string.
|
|
|
|
*
|
2011-01-06 03:06:52 +00:00
|
|
|
* See http://php.net/manual/en/function.strftime.php for information on formatting
|
|
|
|
* using locale strings.
|
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $dateString Datetime string or Unix timestamp
|
|
|
|
* @param int $userOffset User's offset from GMT (in hours)
|
2011-01-06 03:06:52 +00:00
|
|
|
* @param string $format The format to use. If null, `TimeHelper::$niceFormat` is used
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Formatted date string
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1471/Formatting
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-01-06 03:06:52 +00:00
|
|
|
public function nice($dateString = null, $userOffset = null, $format = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($dateString != null) {
|
|
|
|
$date = $this->fromString($dateString, $userOffset);
|
|
|
|
} else {
|
|
|
|
$date = time();
|
|
|
|
}
|
2011-01-06 03:06:52 +00:00
|
|
|
if (!$format) {
|
|
|
|
$format = $this->niceFormat;
|
|
|
|
}
|
|
|
|
$format = $this->convertSpecifiers($format, $date);
|
2010-01-14 20:08:48 +00:00
|
|
|
return strftime($format, $date);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +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 $dateString's year is the current year, the returned string does not
|
|
|
|
* include mention of the year.
|
|
|
|
*
|
|
|
|
* @param string $dateString Datetime string or Unix timestamp
|
|
|
|
* @param int $userOffset User's offset from GMT (in hours)
|
|
|
|
* @return string Described, relative date string
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1471/Formatting
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function niceShort($dateString = null, $userOffset = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$date = $dateString ? $this->fromString($dateString, $userOffset) : time();
|
|
|
|
|
2010-01-14 18:41:57 +00:00
|
|
|
$y = $this->isThisYear($date) ? '' : ' %Y';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-01-06 03:53:56 +00:00
|
|
|
if ($this->isToday($dateString, $userOffset)) {
|
2011-03-12 19:02:56 +00:00
|
|
|
$ret = __d('cake', 'Today, %s', strftime("%H:%M", $date));
|
2011-01-06 03:53:56 +00:00
|
|
|
} elseif ($this->wasYesterday($dateString, $userOffset)) {
|
2011-03-12 19:02:56 +00:00
|
|
|
$ret = __d('cake', 'Yesterday, %s', strftime("%H:%M", $date));
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2010-01-14 20:08:48 +00:00
|
|
|
$format = $this->convertSpecifiers("%b %eS{$y}, %H:%M", $date);
|
2010-01-14 18:41:57 +00:00
|
|
|
$ret = strftime($format, $date);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-01-14 18:41:57 +00:00
|
|
|
|
2010-01-14 20:08:48 +00:00
|
|
|
return $ret;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a partial SQL string to search for all records between two dates.
|
|
|
|
*
|
|
|
|
* @param string $dateString Datetime string or Unix timestamp
|
|
|
|
* @param string $end Datetime string or Unix timestamp
|
|
|
|
* @param string $fieldName Name of database field to compare with
|
|
|
|
* @param int $userOffset User's offset from GMT (in hours)
|
|
|
|
* @return string Partial SQL string.
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1471/Formatting
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function daysAsSql($begin, $end, $fieldName, $userOffset = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$begin = $this->fromString($begin, $userOffset);
|
|
|
|
$end = $this->fromString($end, $userOffset);
|
|
|
|
$begin = date('Y-m-d', $begin) . ' 00:00:00';
|
|
|
|
$end = date('Y-m-d', $end) . ' 23:59:59';
|
|
|
|
|
2010-01-14 20:08:48 +00:00
|
|
|
return "($fieldName >= '$begin') AND ($fieldName <= '$end')";
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a partial SQL string to search for all records between two times
|
|
|
|
* occurring on the same day.
|
|
|
|
*
|
|
|
|
* @param string $dateString Datetime string or Unix timestamp
|
|
|
|
* @param string $fieldName Name of database field to compare with
|
|
|
|
* @param int $userOffset User's offset from GMT (in hours)
|
|
|
|
* @return string Partial SQL string.
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1471/Formatting
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function dayAsSql($dateString, $fieldName, $userOffset = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$date = $this->fromString($dateString, $userOffset);
|
2010-01-14 20:12:30 +00:00
|
|
|
return $this->daysAsSql($dateString, $dateString, $fieldName);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns true if given datetime string is today.
|
|
|
|
*
|
|
|
|
* @param string $dateString Datetime string or Unix timestamp
|
|
|
|
* @param int $userOffset User's offset from GMT (in hours)
|
|
|
|
* @return boolean True if datetime string is today
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function isToday($dateString, $userOffset = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$date = $this->fromString($dateString, $userOffset);
|
|
|
|
return date('Y-m-d', $date) == date('Y-m-d', time());
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-12-31 21:37:16 +00:00
|
|
|
* Returns true if given datetime string is within this week.
|
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $dateString
|
|
|
|
* @param int $userOffset User's offset from GMT (in hours)
|
|
|
|
* @return boolean True if datetime string is within current week
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1472/Testing-Time
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function isThisWeek($dateString, $userOffset = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$date = $this->fromString($dateString, $userOffset);
|
2010-12-31 21:37:16 +00:00
|
|
|
return date('W o', $date) == date('W o', time());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns true if given datetime string is within this month
|
|
|
|
* @param string $dateString
|
|
|
|
* @param int $userOffset User's offset from GMT (in hours)
|
|
|
|
* @return boolean True if datetime string is within current month
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1472/Testing-Time
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function isThisMonth($dateString, $userOffset = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$date = $this->fromString($dateString);
|
|
|
|
return date('m Y',$date) == date('m Y', time());
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns true if given datetime string is within current year.
|
|
|
|
*
|
|
|
|
* @param string $dateString Datetime string or Unix timestamp
|
|
|
|
* @return boolean True if datetime string is within current year
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1472/Testing-Time
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function isThisYear($dateString, $userOffset = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$date = $this->fromString($dateString, $userOffset);
|
|
|
|
return date('Y', $date) == date('Y', time());
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns true if given datetime string was yesterday.
|
|
|
|
*
|
|
|
|
* @param string $dateString Datetime string or Unix timestamp
|
|
|
|
* @param int $userOffset User's offset from GMT (in hours)
|
|
|
|
* @return boolean True if datetime string was yesterday
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1472/Testing-Time
|
2011-02-21 19:54:21 +00:00
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function wasYesterday($dateString, $userOffset = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$date = $this->fromString($dateString, $userOffset);
|
|
|
|
return date('Y-m-d', $date) == date('Y-m-d', strtotime('yesterday'));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns true if given datetime string is tomorrow.
|
|
|
|
*
|
|
|
|
* @param string $dateString Datetime string or Unix timestamp
|
|
|
|
* @param int $userOffset User's offset from GMT (in hours)
|
|
|
|
* @return boolean True if datetime string was yesterday
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1472/Testing-Time
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function isTomorrow($dateString, $userOffset = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$date = $this->fromString($dateString, $userOffset);
|
|
|
|
return date('Y-m-d', $date) == date('Y-m-d', strtotime('tomorrow'));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 22:59:05 +00:00
|
|
|
* Returns the quarter
|
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $dateString
|
|
|
|
* @param boolean $range if true returns a range in Y-m-d format
|
|
|
|
* @return boolean True if datetime string is within current week
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1471/Formatting
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function toQuarter($dateString, $range = false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$time = $this->fromString($dateString);
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
2010-01-14 20:08:48 +00:00
|
|
|
return $date;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a UNIX timestamp from a textual datetime description. Wrapper for PHP function strtotime().
|
|
|
|
*
|
|
|
|
* @param string $dateString Datetime string to be represented as a Unix timestamp
|
|
|
|
* @param int $userOffset User's offset from GMT (in hours)
|
|
|
|
* @return integer Unix timestamp
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1471/Formatting
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function toUnix($dateString, $userOffset = null) {
|
2010-01-14 20:12:30 +00:00
|
|
|
return $this->fromString($dateString, $userOffset);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a date formatted for Atom 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
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1471/Formatting
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function toAtom($dateString, $userOffset = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$date = $this->fromString($dateString, $userOffset);
|
2010-01-14 20:12:30 +00:00
|
|
|
return date('Y-m-d\TH:i:s\Z', $date);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1471/Formatting
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function toRSS($dateString, $userOffset = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$date = $this->fromString($dateString, $userOffset);
|
2010-01-14 20:12:30 +00:00
|
|
|
return date("r", $date);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns either a relative date or a formatted date depending
|
|
|
|
* on the difference between the current time and given datetime.
|
2009-02-03 18:48:28 +00:00
|
|
|
* $datetime should be in a <i>strtotime</i> - parsable format, like MySQL's datetime datatype.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* ### Options:
|
2009-03-12 17:33:47 +00:00
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* - `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)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Relative dates look something like this:
|
|
|
|
* 3 weeks, 4 days ago
|
|
|
|
* 15 seconds ago
|
2009-08-13 17:57:15 +00:00
|
|
|
*
|
|
|
|
* Default date formatting is d/m/yy e.g: on 18/2/09
|
2008-05-30 11:40:08 +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 $dateString Datetime string or Unix timestamp
|
|
|
|
* @param array $options Default format if timestamp is used in $dateString
|
|
|
|
* @return string Relative time string.
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1471/Formatting
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function timeAgoInWords($dateTime, $options = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$userOffset = null;
|
|
|
|
if (is_array($options) && isset($options['userOffset'])) {
|
|
|
|
$userOffset = $options['userOffset'];
|
|
|
|
}
|
2008-07-10 03:30:28 +00:00
|
|
|
$now = time();
|
|
|
|
if (!is_null($userOffset)) {
|
2010-03-09 03:49:09 +00:00
|
|
|
$now = $this->convert(time(), $userOffset);
|
2008-07-10 03:30:28 +00:00
|
|
|
}
|
2008-10-23 00:10:44 +00:00
|
|
|
$inSeconds = $this->fromString($dateTime, $userOffset);
|
|
|
|
$backwards = ($inSeconds > $now);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$format = 'j/n/y';
|
|
|
|
$end = '+1 month';
|
2008-08-01 00:48:14 +00:00
|
|
|
|
2008-05-30 11:40:08 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($backwards) {
|
2008-10-23 00:10:44 +00:00
|
|
|
$futureTime = $inSeconds;
|
|
|
|
$pastTime = $now;
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2008-10-23 00:10:44 +00:00
|
|
|
$futureTime = $now;
|
|
|
|
$pastTime = $inSeconds;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-10-23 00:10:44 +00:00
|
|
|
$diff = $futureTime - $pastTime;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
// If more than a week, then take into account the length of months
|
|
|
|
if ($diff >= 604800) {
|
|
|
|
$current = array();
|
|
|
|
$date = array();
|
2008-08-01 00:48:14 +00:00
|
|
|
|
2008-10-23 00:10:44 +00:00
|
|
|
list($future['H'], $future['i'], $future['s'], $future['d'], $future['m'], $future['Y']) = explode('/', date('H/i/s/d/m/Y', $futureTime));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-10-23 00:10:44 +00:00
|
|
|
list($past['H'], $past['i'], $past['s'], $past['d'], $past['m'], $past['Y']) = explode('/', date('H/i/s/d/m/Y', $pastTime));
|
2008-05-30 11:40:08 +00:00
|
|
|
$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']);
|
2008-08-01 00:48:14 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($months >= 12) {
|
|
|
|
$years = floor($months / 12);
|
|
|
|
$months = $months - ($years * 12);
|
|
|
|
}
|
2008-08-01 00:48:14 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($future['m'] < $past['m'] && $future['Y'] - $past['Y'] == 1) {
|
|
|
|
$years --;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($future['d'] >= $past['d']) {
|
|
|
|
$days = $future['d'] - $past['d'];
|
|
|
|
} else {
|
2008-10-23 00:10:44 +00:00
|
|
|
$daysInPastMonth = date('t', $pastTime);
|
|
|
|
$daysInFutureMonth = date('t', mktime(0, 0, 0, $future['m'] - 1, 1, $future['Y']));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (!$backwards) {
|
2008-10-23 00:10:44 +00:00
|
|
|
$days = ($daysInPastMonth - $past['d']) + $future['d'];
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2008-10-23 00:10:44 +00:00
|
|
|
$days = ($daysInFutureMonth - $past['d']) + $future['d'];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($future['m'] != $past['m']) {
|
|
|
|
$months --;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-23 00:10:44 +00:00
|
|
|
if ($months == 0 && $years >= 1 && $diff < ($years * 31536000)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$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;
|
|
|
|
}
|
2008-10-23 00:10:44 +00:00
|
|
|
$relativeDate = '';
|
|
|
|
$diff = $futureTime - $pastTime;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($diff > abs($now - $this->fromString($end))) {
|
2011-03-12 19:02:56 +00:00
|
|
|
$relativeDate = __d('cake', 'on %s', date($format, $inSeconds));
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
if ($years > 0) {
|
|
|
|
// years and months and days
|
2011-05-04 05:40:23 +00:00
|
|
|
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d year', '%d years', $years, $years);
|
|
|
|
$relativeDate .= $months > 0 ? ($relativeDate ? ', ' : '') . __dn('cake', '%d month', '%d months', $months, $months) : '';
|
|
|
|
$relativeDate .= $weeks > 0 ? ($relativeDate ? ', ' : '') . __dn('cake', '%d week', '%d weeks', $weeks, $weeks) : '';
|
|
|
|
$relativeDate .= $days > 0 ? ($relativeDate ? ', ' : '') . __dn('cake', '%d day', '%d days', $days, $days) : '';
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif (abs($months) > 0) {
|
|
|
|
// months, weeks and days
|
2011-05-04 05:40:23 +00:00
|
|
|
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d month', '%d months', $months, $months);
|
|
|
|
$relativeDate .= $weeks > 0 ? ($relativeDate ? ', ' : '') . __dn('cake', '%d week', '%d weeks', $weeks, $weeks) : '';
|
|
|
|
$relativeDate .= $days > 0 ? ($relativeDate ? ', ' : '') . __dn('cake', '%d day', '%d days', $days, $days) : '';
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif (abs($weeks) > 0) {
|
|
|
|
// weeks and days
|
2011-05-04 05:40:23 +00:00
|
|
|
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d week', '%d weeks', $weeks, $weeks);
|
|
|
|
$relativeDate .= $days > 0 ? ($relativeDate ? ', ' : '') . __dn('cake', '%d day', '%d days', $days, $days) : '';
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif (abs($days) > 0) {
|
|
|
|
// days and hours
|
2011-05-04 05:40:23 +00:00
|
|
|
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d day', '%d days', $days, $days);
|
|
|
|
$relativeDate .= $hours > 0 ? ($relativeDate ? ', ' : '') . __dn('cake', '%d hour', '%d hours', $hours, $hours) : '';
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif (abs($hours) > 0) {
|
|
|
|
// hours and minutes
|
2011-05-04 05:40:23 +00:00
|
|
|
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d hour', '%d hours', $hours, $hours);
|
|
|
|
$relativeDate .= $minutes > 0 ? ($relativeDate ? ', ' : '') . __dn('cake', '%d minute', '%d minutes', $minutes, $minutes) : '';
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif (abs($minutes) > 0) {
|
|
|
|
// minutes only
|
2011-05-04 05:40:23 +00:00
|
|
|
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d minute', '%d minutes', $minutes, $minutes);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
// seconds only
|
2011-05-04 05:40:23 +00:00
|
|
|
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d second', '%d seconds', $seconds, $seconds);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$backwards) {
|
2011-03-12 19:02:56 +00:00
|
|
|
$relativeDate = __d('cake', '%s ago', $relativeDate);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2010-01-14 20:08:48 +00:00
|
|
|
return $relativeDate;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns true if specified datetime was within the interval specified, else false.
|
|
|
|
*
|
2011-02-21 19:54:21 +00:00
|
|
|
* @param mixed $timeInterval the numeric value with space then time type.
|
2010-01-25 22:59:05 +00:00
|
|
|
* Example of valid types: 6 hours, 2 days, 1 minute.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param mixed $dateString the datestring or unix timestamp to compare
|
2008-07-10 03:30:28 +00:00
|
|
|
* @param int $userOffset User's offset from GMT (in hours)
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return bool
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1472/Testing-Time
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function wasWithinLast($timeInterval, $dateString, $userOffset = null) {
|
2009-07-23 20:51:24 +00:00
|
|
|
$tmp = str_replace(' ', '', $timeInterval);
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_numeric($tmp)) {
|
2011-03-12 19:02:56 +00:00
|
|
|
$timeInterval = $tmp . ' ' . __d('cake', 'days');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$date = $this->fromString($dateString, $userOffset);
|
|
|
|
$interval = $this->fromString('-'.$timeInterval);
|
|
|
|
|
|
|
|
if ($date >= $interval && $date <= time()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns gmt, given either a UNIX timestamp or a valid strtotime() date string.
|
|
|
|
*
|
|
|
|
* @param string $dateString Datetime string
|
|
|
|
* @return string Formatted date string
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1471/Formatting
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function gmt($string = null) {
|
2008-05-30 11:40:08 +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));
|
|
|
|
|
2010-01-14 20:12:30 +00:00
|
|
|
return gmmktime($hour, $minute, $second, $month, $day, $year);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-09-20 09:24:13 +00:00
|
|
|
* Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string.
|
2010-01-14 20:20:27 +00:00
|
|
|
* This function also accepts a time string and a format string as first and second parameters.
|
|
|
|
* In that case this function behaves as a wrapper for TimeHelper::i18nFormat()
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-01-14 20:20:27 +00:00
|
|
|
* @param string $format date format string (or a DateTime string)
|
|
|
|
* @param string $dateString Datetime string (or a date format string)
|
2008-05-30 11:40:08 +00:00
|
|
|
* @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
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function format($format, $date = null, $invalid = false, $userOffset = null) {
|
2010-01-14 18:41:57 +00:00
|
|
|
$time = $this->fromString($date, $userOffset);
|
|
|
|
$_time = $this->fromString($format, $userOffset);
|
|
|
|
|
|
|
|
if (is_numeric($_time) && $time === false) {
|
|
|
|
$format = $date;
|
2010-01-14 20:08:48 +00:00
|
|
|
return $this->i18nFormat($_time, $format, $invalid, $userOffset);
|
2010-01-14 18:41:57 +00:00
|
|
|
}
|
|
|
|
if ($time === false && $invalid !== false) {
|
|
|
|
return $invalid;
|
|
|
|
}
|
|
|
|
return date($format, $time);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string.
|
|
|
|
* It take in account the default date format for the current language if a LC_TIME file is used.
|
2010-01-25 22:59:05 +00:00
|
|
|
*
|
2010-01-14 18:41:57 +00:00
|
|
|
* @param string $dateString Datetime string
|
|
|
|
* @param string $format strftime format string.
|
|
|
|
* @param boolean $invalid flag to ignore results of fromString == false
|
|
|
|
* @param int $userOffset User's offset from GMT (in hours)
|
2010-01-25 22:59:05 +00:00
|
|
|
* @return string Formatted and translated date string @access public
|
2010-01-14 18:41:57 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function i18nFormat($date, $format = null, $invalid = false, $userOffset = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$date = $this->fromString($date, $userOffset);
|
|
|
|
if ($date === false && $invalid !== false) {
|
|
|
|
return $invalid;
|
2008-08-01 00:48:14 +00:00
|
|
|
}
|
2010-01-14 18:41:57 +00:00
|
|
|
if (empty($format)) {
|
|
|
|
$format = '%x';
|
|
|
|
}
|
2010-01-14 20:08:48 +00:00
|
|
|
$format = $this->convertSpecifiers($format, $date);
|
|
|
|
return strftime($format, $date);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|