|
|
@@ -36,7 +36,7 @@ class CakeTime {
|
|
|
* `strftime` (http://php.net/manual/en/function.strftime.php)
|
|
|
*
|
|
|
* @var string
|
|
|
- * @see TimeHelper::format()
|
|
|
+ * @see CakeTime::format()
|
|
|
*/
|
|
|
public static $niceFormat = '%a, %b %eS %Y, %H:%M';
|
|
|
|
|
|
@@ -195,15 +195,23 @@ class CakeTime {
|
|
|
* Converts given time (in server's time zone) to user's local time, given his/her offset from GMT.
|
|
|
*
|
|
|
* @param string $serverTime UNIX timestamp
|
|
|
- * @param integer $userOffset User's offset from GMT (in hours)
|
|
|
+ * @param mixed $timezone User's timezone string or DateTimeZone object
|
|
|
* @return integer UNIX timestamp
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
|
|
|
*/
|
|
|
- public static function convert($serverTime, $userOffset) {
|
|
|
+ public static function convert($serverTime, $timezone) {
|
|
|
$serverOffset = self::serverOffset();
|
|
|
$gmtTime = $serverTime - $serverOffset;
|
|
|
- $userTime = $gmtTime + $userOffset * (60 * 60);
|
|
|
- return $userTime;
|
|
|
+ if (is_numeric($timezone)) {
|
|
|
+ $userOffset = $timezone * (60 * 60);
|
|
|
+ } else {
|
|
|
+ if (!is_object($timezone)) {
|
|
|
+ $timezone = new DateTimeZone($timezone);
|
|
|
+ }
|
|
|
+ $userOffset = $timezone->getOffset(new DateTime('@' . $gmtTime));
|
|
|
+ }
|
|
|
+ $userTime = $gmtTime + $userOffset;
|
|
|
+ return (int)$userTime;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -220,21 +228,23 @@ class CakeTime {
|
|
|
* Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string.
|
|
|
*
|
|
|
* @param string $dateString Datetime string
|
|
|
- * @param integer $userOffset User's offset from GMT (in hours)
|
|
|
+ * @param mixed $timezone Timezone string or DateTimezone object
|
|
|
* @return string Parsed timestamp
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
|
|
|
*/
|
|
|
- public static function fromString($dateString, $userOffset = null) {
|
|
|
+ public static function fromString($dateString, $timezone = null) {
|
|
|
if (empty($dateString)) {
|
|
|
return false;
|
|
|
}
|
|
|
+
|
|
|
if (is_integer($dateString) || is_numeric($dateString)) {
|
|
|
$date = intval($dateString);
|
|
|
} else {
|
|
|
$date = strtotime($dateString);
|
|
|
}
|
|
|
- if ($userOffset !== null) {
|
|
|
- return self::convert($date, $userOffset);
|
|
|
+
|
|
|
+ if ($timezone !== null) {
|
|
|
+ return self::convert($date, $timezone);
|
|
|
}
|
|
|
if ($date === -1) {
|
|
|
return false;
|
|
|
@@ -249,14 +259,14 @@ class CakeTime {
|
|
|
* using locale strings.
|
|
|
*
|
|
|
* @param string $dateString Datetime string or Unix timestamp
|
|
|
- * @param integer $userOffset User's offset from GMT (in hours)
|
|
|
+ * @param mixed $timezone Timezone string or DateTimezone object
|
|
|
* @param string $format The format to use. If null, `TimeHelper::$niceFormat` is used
|
|
|
* @return string Formatted date string
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
|
|
|
*/
|
|
|
- public static function nice($dateString = null, $userOffset = null, $format = null) {
|
|
|
+ public static function nice($dateString = null, $timezone = null, $format = null) {
|
|
|
if ($dateString != null) {
|
|
|
- $date = self::fromString($dateString, $userOffset);
|
|
|
+ $date = self::fromString($dateString, $timezone);
|
|
|
} else {
|
|
|
$date = time();
|
|
|
}
|
|
|
@@ -276,18 +286,18 @@ class CakeTime {
|
|
|
* include mention of the year.
|
|
|
*
|
|
|
* @param string $dateString Datetime string or Unix timestamp
|
|
|
- * @param integer $userOffset User's offset from GMT (in hours)
|
|
|
+ * @param mixed $timezone Timezone string or DateTimezone object
|
|
|
* @return string Described, relative date string
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
|
|
|
*/
|
|
|
- public static function niceShort($dateString = null, $userOffset = null) {
|
|
|
- $date = $dateString ? self::fromString($dateString, $userOffset) : time();
|
|
|
+ public static function niceShort($dateString = null, $timezone = null) {
|
|
|
+ $date = $dateString ? self::fromString($dateString, $timezone) : time();
|
|
|
|
|
|
$y = self::isThisYear($date) ? '' : ' %Y';
|
|
|
|
|
|
- if (self::isToday($dateString, $userOffset)) {
|
|
|
+ if (self::isToday($dateString, $timezone)) {
|
|
|
$ret = __d('cake', 'Today, %s', self::_strftime("%H:%M", $date));
|
|
|
- } elseif (self::wasYesterday($dateString, $userOffset)) {
|
|
|
+ } elseif (self::wasYesterday($dateString, $timezone)) {
|
|
|
$ret = __d('cake', 'Yesterday, %s', self::_strftime("%H:%M", $date));
|
|
|
} else {
|
|
|
$format = self::convertSpecifiers("%b %eS{$y}, %H:%M", $date);
|
|
|
@@ -303,13 +313,13 @@ class CakeTime {
|
|
|
* @param string $begin Datetime string or Unix timestamp
|
|
|
* @param string $end Datetime string or Unix timestamp
|
|
|
* @param string $fieldName Name of database field to compare with
|
|
|
- * @param integer $userOffset User's offset from GMT (in hours)
|
|
|
+ * @param mixed $timezone Timezone string or DateTimezone object
|
|
|
* @return string Partial SQL string.
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
|
|
|
*/
|
|
|
- public static function daysAsSql($begin, $end, $fieldName, $userOffset = null) {
|
|
|
- $begin = self::fromString($begin, $userOffset);
|
|
|
- $end = self::fromString($end, $userOffset);
|
|
|
+ public static function daysAsSql($begin, $end, $fieldName, $timezone = null) {
|
|
|
+ $begin = self::fromString($begin, $timezone);
|
|
|
+ $end = self::fromString($end, $timezone);
|
|
|
$begin = date('Y-m-d', $begin) . ' 00:00:00';
|
|
|
$end = date('Y-m-d', $end) . ' 23:59:59';
|
|
|
|
|
|
@@ -322,11 +332,11 @@ class CakeTime {
|
|
|
*
|
|
|
* @param string $dateString Datetime string or Unix timestamp
|
|
|
* @param string $fieldName Name of database field to compare with
|
|
|
- * @param integer $userOffset User's offset from GMT (in hours)
|
|
|
+ * @param mixed $timezone Timezone string or DateTimezone object
|
|
|
* @return string Partial SQL string.
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
|
|
|
*/
|
|
|
- public static function dayAsSql($dateString, $fieldName, $userOffset = null) {
|
|
|
+ public static function dayAsSql($dateString, $fieldName, $timezone = null) {
|
|
|
return self::daysAsSql($dateString, $dateString, $fieldName);
|
|
|
}
|
|
|
|
|
|
@@ -334,12 +344,12 @@ class CakeTime {
|
|
|
* Returns true if given datetime string is today.
|
|
|
*
|
|
|
* @param string $dateString Datetime string or Unix timestamp
|
|
|
- * @param integer $userOffset User's offset from GMT (in hours)
|
|
|
+ * @param mixed $timezone Timezone string or DateTimezone object
|
|
|
* @return boolean True if datetime string is today
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
|
|
|
*/
|
|
|
- public static function isToday($dateString, $userOffset = null) {
|
|
|
- $date = self::fromString($dateString, $userOffset);
|
|
|
+ public static function isToday($dateString, $timezone = null) {
|
|
|
+ $date = self::fromString($dateString, $timezone);
|
|
|
return date('Y-m-d', $date) == date('Y-m-d', time());
|
|
|
}
|
|
|
|
|
|
@@ -347,23 +357,23 @@ class CakeTime {
|
|
|
* Returns true if given datetime string is within this week.
|
|
|
*
|
|
|
* @param string $dateString
|
|
|
- * @param integer $userOffset User's offset from GMT (in hours)
|
|
|
+ * @param mixed $timezone Timezone string or DateTimezone object
|
|
|
* @return boolean True if datetime string is within current week
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
|
|
|
*/
|
|
|
- public static function isThisWeek($dateString, $userOffset = null) {
|
|
|
- $date = self::fromString($dateString, $userOffset);
|
|
|
+ public static function isThisWeek($dateString, $timezone = null) {
|
|
|
+ $date = self::fromString($dateString, $timezone);
|
|
|
return date('W o', $date) == date('W o', time());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Returns true if given datetime string is within this month
|
|
|
* @param string $dateString
|
|
|
- * @param integer $userOffset User's offset from GMT (in hours)
|
|
|
+ * @param mixed $timezone Timezone string or DateTimezone object
|
|
|
* @return boolean True if datetime string is within current month
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
|
|
|
*/
|
|
|
- public static function isThisMonth($dateString, $userOffset = null) {
|
|
|
+ public static function isThisMonth($dateString, $timezone = null) {
|
|
|
$date = self::fromString($dateString);
|
|
|
return date('m Y', $date) == date('m Y', time());
|
|
|
}
|
|
|
@@ -372,12 +382,12 @@ class CakeTime {
|
|
|
* Returns true if given datetime string is within current year.
|
|
|
*
|
|
|
* @param string $dateString Datetime string or Unix timestamp
|
|
|
- * @param integer $userOffset User's offset from GMT (in hours)
|
|
|
+ * @param mixed $timezone Timezone string or DateTimezone object
|
|
|
* @return boolean True if datetime string is within current year
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
|
|
|
*/
|
|
|
- public static function isThisYear($dateString, $userOffset = null) {
|
|
|
- $date = self::fromString($dateString, $userOffset);
|
|
|
+ public static function isThisYear($dateString, $timezone = null) {
|
|
|
+ $date = self::fromString($dateString, $timezone);
|
|
|
return date('Y', $date) == date('Y', time());
|
|
|
}
|
|
|
|
|
|
@@ -385,13 +395,13 @@ class CakeTime {
|
|
|
* Returns true if given datetime string was yesterday.
|
|
|
*
|
|
|
* @param string $dateString Datetime string or Unix timestamp
|
|
|
- * @param integer $userOffset User's offset from GMT (in hours)
|
|
|
+ * @param mixed $timezone Timezone string or DateTimezone object
|
|
|
* @return boolean True if datetime string was yesterday
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
|
|
|
*
|
|
|
*/
|
|
|
- public static function wasYesterday($dateString, $userOffset = null) {
|
|
|
- $date = self::fromString($dateString, $userOffset);
|
|
|
+ public static function wasYesterday($dateString, $timezone = null) {
|
|
|
+ $date = self::fromString($dateString, $timezone);
|
|
|
return date('Y-m-d', $date) == date('Y-m-d', strtotime('yesterday'));
|
|
|
}
|
|
|
|
|
|
@@ -399,12 +409,12 @@ class CakeTime {
|
|
|
* Returns true if given datetime string is tomorrow.
|
|
|
*
|
|
|
* @param string $dateString Datetime string or Unix timestamp
|
|
|
- * @param integer $userOffset User's offset from GMT (in hours)
|
|
|
+ * @param mixed $timezone Timezone string or DateTimezone object
|
|
|
* @return boolean True if datetime string was yesterday
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
|
|
|
*/
|
|
|
- public static function isTomorrow($dateString, $userOffset = null) {
|
|
|
- $date = self::fromString($dateString, $userOffset);
|
|
|
+ public static function isTomorrow($dateString, $timezone = null) {
|
|
|
+ $date = self::fromString($dateString, $timezone);
|
|
|
return date('Y-m-d', $date) == date('Y-m-d', strtotime('tomorrow'));
|
|
|
}
|
|
|
|
|
|
@@ -449,24 +459,24 @@ class CakeTime {
|
|
|
* 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 integer $userOffset User's offset from GMT (in hours)
|
|
|
+ * @param mixed $timezone Timezone string or DateTimezone object
|
|
|
* @return integer Unix timestamp
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
|
|
|
*/
|
|
|
- public static function toUnix($dateString, $userOffset = null) {
|
|
|
- return self::fromString($dateString, $userOffset);
|
|
|
+ public static function toUnix($dateString, $timezone = null) {
|
|
|
+ return self::fromString($dateString, $timezone);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Returns a date formatted for Atom RSS feeds.
|
|
|
*
|
|
|
* @param string $dateString Datetime string or Unix timestamp
|
|
|
- * @param integer $userOffset User's offset from GMT (in hours)
|
|
|
+ * @param mixed $timezone Timezone string or DateTimezone object
|
|
|
* @return string Formatted date string
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
|
|
|
*/
|
|
|
- public static function toAtom($dateString, $userOffset = null) {
|
|
|
- $date = self::fromString($dateString, $userOffset);
|
|
|
+ public static function toAtom($dateString, $timezone = null) {
|
|
|
+ $date = self::fromString($dateString, $timezone);
|
|
|
return date('Y-m-d\TH:i:s\Z', $date);
|
|
|
}
|
|
|
|
|
|
@@ -474,14 +484,24 @@ class CakeTime {
|
|
|
* Formats date for RSS feeds
|
|
|
*
|
|
|
* @param string $dateString Datetime string or Unix timestamp
|
|
|
- * @param integer $userOffset User's offset from GMT (in hours)
|
|
|
+ * @param mixed $timezone Timezone string or DateTimezone object
|
|
|
* @return string Formatted date string
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
|
|
|
*/
|
|
|
- public static function toRSS($dateString, $userOffset = null) {
|
|
|
- $date = self::fromString($dateString, $userOffset);
|
|
|
+ public static function toRSS($dateString, $timezone = null) {
|
|
|
+ $date = self::fromString($dateString, $timezone);
|
|
|
|
|
|
- if (!is_null($userOffset)) {
|
|
|
+ if (!is_null($timezone)) {
|
|
|
+ if (is_numeric($timezone)) {
|
|
|
+ $userOffset = $timezone;
|
|
|
+ } else {
|
|
|
+ if (!is_object($timezone)) {
|
|
|
+ $timezone = new DateTimeZone($timezone);
|
|
|
+ }
|
|
|
+ $currentDate = new DateTime('@' . $date);
|
|
|
+ $currentDate->setTimezone($timezone);
|
|
|
+ $userOffset = $timezone->getOffset($currentDate) / 60 / 60;
|
|
|
+ }
|
|
|
if ($userOffset == 0) {
|
|
|
$timezone = '+0000';
|
|
|
} else {
|
|
|
@@ -520,15 +540,16 @@ class CakeTime {
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
|
|
|
*/
|
|
|
public static function timeAgoInWords($dateTime, $options = array()) {
|
|
|
- $userOffset = null;
|
|
|
- if (is_array($options) && isset($options['userOffset'])) {
|
|
|
- $userOffset = $options['userOffset'];
|
|
|
- }
|
|
|
- $now = time();
|
|
|
- if (!is_null($userOffset)) {
|
|
|
- $now = self::convert(time(), $userOffset);
|
|
|
+ $timezone = null;
|
|
|
+ if (is_array($options)) {
|
|
|
+ if (isset($options['userOffset'])) {
|
|
|
+ $timezone = $options['userOffset'];
|
|
|
+ } elseif (isset($options['timezone'])) {
|
|
|
+ $timezone = $options['timezone'];
|
|
|
+ }
|
|
|
}
|
|
|
- $inSeconds = self::fromString($dateTime, $userOffset);
|
|
|
+ $now = self::fromString(time(), $timezone);
|
|
|
+ $inSeconds = self::fromString($dateTime, $timezone);
|
|
|
$backwards = ($inSeconds > $now);
|
|
|
|
|
|
$format = 'j/n/y';
|
|
|
@@ -678,17 +699,17 @@ class CakeTime {
|
|
|
* @param mixed $timeInterval the numeric value with space then time type.
|
|
|
* Example of valid types: 6 hours, 2 days, 1 minute.
|
|
|
* @param mixed $dateString the datestring or unix timestamp to compare
|
|
|
- * @param integer $userOffset User's offset from GMT (in hours)
|
|
|
+ * @param mixed $timezone Timezone string or DateTimezone object
|
|
|
* @return boolean
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
|
|
|
*/
|
|
|
- public static function wasWithinLast($timeInterval, $dateString, $userOffset = null) {
|
|
|
+ public static function wasWithinLast($timeInterval, $dateString, $timezone = null) {
|
|
|
$tmp = str_replace(' ', '', $timeInterval);
|
|
|
if (is_numeric($tmp)) {
|
|
|
$timeInterval = $tmp . ' ' . __d('cake', 'days');
|
|
|
}
|
|
|
|
|
|
- $date = self::fromString($dateString, $userOffset);
|
|
|
+ $date = self::fromString($dateString, $timezone);
|
|
|
$interval = self::fromString('-' . $timeInterval);
|
|
|
|
|
|
if ($date >= $interval && $date <= time()) {
|
|
|
@@ -729,17 +750,17 @@ class CakeTime {
|
|
|
* @param string $format date format string (or a DateTime string)
|
|
|
* @param string $date Datetime string (or a date format string)
|
|
|
* @param boolean $invalid flag to ignore results of fromString == false
|
|
|
- * @param integer $userOffset User's offset from GMT (in hours)
|
|
|
+ * @param mixed $timezone Timezone string or DateTimezone object
|
|
|
* @return string Formatted date string
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
|
|
|
*/
|
|
|
- public static function format($format, $date = null, $invalid = false, $userOffset = null) {
|
|
|
- $time = self::fromString($date, $userOffset);
|
|
|
- $_time = self::fromString($format, $userOffset);
|
|
|
+ public static function format($format, $date = null, $invalid = false, $timezone = null) {
|
|
|
+ $time = self::fromString($date, $timezone);
|
|
|
+ $_time = self::fromString($format, $timezone);
|
|
|
|
|
|
if (is_numeric($_time) && $time === false) {
|
|
|
$format = $date;
|
|
|
- return self::i18nFormat($_time, $format, $invalid, $userOffset);
|
|
|
+ return self::i18nFormat($_time, $format, $invalid, $timezone);
|
|
|
}
|
|
|
if ($time === false && $invalid !== false) {
|
|
|
return $invalid;
|
|
|
@@ -754,12 +775,12 @@ class CakeTime {
|
|
|
* @param string $date Datetime string
|
|
|
* @param string $format strftime format string.
|
|
|
* @param boolean $invalid flag to ignore results of fromString == false
|
|
|
- * @param integer $userOffset User's offset from GMT (in hours)
|
|
|
+ * @param mixed $timezone Timezone string or DateTimezone object
|
|
|
* @return string Formatted and translated date string
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
|
|
|
*/
|
|
|
- public static function i18nFormat($date, $format = null, $invalid = false, $userOffset = null) {
|
|
|
- $date = self::fromString($date, $userOffset);
|
|
|
+ public static function i18nFormat($date, $format = null, $invalid = false, $timezone = null) {
|
|
|
+ $date = self::fromString($date, $timezone);
|
|
|
if ($date === false && $invalid !== false) {
|
|
|
return $invalid;
|
|
|
}
|