PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/QuickApps/Cake/View/Helper/TimeHelper.php

http://github.com/QuickAppsCMS/QuickApps-CMS
PHP | 408 lines | 120 code | 32 blank | 256 comment | 7 complexity | 60407607ff484c4c24f5aaa1bcac3835 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-3.0
  1. <?php
  2. /**
  3. * Time Helper class file.
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.View.Helper
  16. * @since CakePHP(tm) v 0.10.0.1076
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('CakeTime', 'Utility');
  20. App::uses('Multibyte', 'I18n');
  21. App::uses('AppHelper', 'View/Helper');
  22. /**
  23. * Time Helper class for easy use of time data.
  24. *
  25. * Manipulation of time data.
  26. *
  27. * @package Cake.View.Helper
  28. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html
  29. * @see CakeTime
  30. */
  31. class TimeHelper extends AppHelper {
  32. /**
  33. * CakeTime instance
  34. */
  35. protected $_engine = null;
  36. /**
  37. * Constructor
  38. *
  39. * ### Settings:
  40. *
  41. * - `engine` Class name to use to replace CakeTime functionality
  42. * The class needs to be placed in the `Utility` directory.
  43. *
  44. * @param View $View the view object the helper is attached to.
  45. * @param array $settings Settings array Settings array
  46. * @throws CakeException When the engine class could not be found.
  47. */
  48. public function __construct(View $View, $settings = array()) {
  49. $settings = Hash::merge(array('engine' => 'CakeTime'), $settings);
  50. parent::__construct($View, $settings);
  51. list($plugin, $engineClass) = pluginSplit($settings['engine'], true);
  52. App::uses($engineClass, $plugin . 'Utility');
  53. if (class_exists($engineClass)) {
  54. $this->_engine = new $engineClass($settings);
  55. } else {
  56. throw new CakeException(__d('cake_dev', '%s could not be found', $engineClass));
  57. }
  58. }
  59. /**
  60. * Magic accessor for deprecated attributes.
  61. *
  62. * @param string $name Name of the attribute to set.
  63. * @param string $value Value of the attribute to set.
  64. * @return mixed
  65. */
  66. public function __set($name, $value) {
  67. switch ($name) {
  68. case 'niceFormat':
  69. $this->_engine->{$name} = $value;
  70. break;
  71. default:
  72. $this->{$name} = $value;
  73. break;
  74. }
  75. }
  76. /**
  77. * Magic isset check for deprecated attributes.
  78. *
  79. * @param string $name Name of the attribute to check.
  80. * @return boolean
  81. */
  82. public function __isset($name) {
  83. if (isset($this->{$name})) {
  84. return true;
  85. }
  86. $magicGet = array('niceFormat');
  87. if (in_array($name, $magicGet)) {
  88. return $this->__get($name) !== null;
  89. }
  90. return null;
  91. }
  92. /**
  93. * Magic accessor for attributes that were deprecated.
  94. *
  95. * @param string $name Name of the attribute to get.
  96. * @return mixed
  97. */
  98. public function __get($name) {
  99. if (isset($this->_engine->{$name})) {
  100. return $this->_engine->{$name};
  101. }
  102. $magicGet = array('niceFormat');
  103. if (in_array($name, $magicGet)) {
  104. return $this->_engine->{$name};
  105. }
  106. return null;
  107. }
  108. /**
  109. * Call methods from CakeTime utility class
  110. */
  111. public function __call($method, $params) {
  112. return call_user_func_array(array($this->_engine, $method), $params);
  113. }
  114. /**
  115. * @see CakeTime::convertSpecifiers()
  116. *
  117. * @param string $format Format with specifiers for strftime function.
  118. * Accepts the special specifier %S which mimics the modifier S for date()
  119. * @param string $time UNIX timestamp
  120. * @return string windows safe and date() function compatible format for strftime
  121. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  122. */
  123. public function convertSpecifiers($format, $time = null) {
  124. return $this->_engine->convertSpecifiers($format, $time);
  125. }
  126. /**
  127. * @see CakeTime::convert()
  128. *
  129. * @param string $serverTime UNIX timestamp
  130. * @param mixed $timezone User's timezone string or DateTimeZone object
  131. * @return integer UNIX timestamp
  132. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  133. */
  134. public function convert($serverTime, $timezone) {
  135. return $this->_engine->convert($serverTime, $timezone);
  136. }
  137. /**
  138. * @see CakeTime::serverOffset()
  139. *
  140. * @return integer Offset
  141. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  142. */
  143. public function serverOffset() {
  144. return $this->_engine->serverOffset();
  145. }
  146. /**
  147. * @see CakeTime::fromString()
  148. *
  149. * @param string $dateString Datetime string
  150. * @param mixed $timezone User's timezone string or DateTimeZone object
  151. * @return string Parsed timestamp
  152. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  153. */
  154. public function fromString($dateString, $timezone = null) {
  155. return $this->_engine->fromString($dateString, $timezone);
  156. }
  157. /**
  158. * @see CakeTime::nice()
  159. *
  160. * @param string $dateString Datetime string or Unix timestamp
  161. * @param mixed $timezone User's timezone string or DateTimeZone object
  162. * @param string $format The format to use. If null, `TimeHelper::$niceFormat` is used
  163. * @return string Formatted date string
  164. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  165. */
  166. public function nice($dateString = null, $timezone = null, $format = null) {
  167. return $this->_engine->nice($dateString, $timezone, $format);
  168. }
  169. /**
  170. * @see CakeTime::niceShort()
  171. *
  172. * @param string $dateString Datetime string or Unix timestamp
  173. * @param mixed $timezone User's timezone string or DateTimeZone object
  174. * @return string Described, relative date string
  175. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  176. */
  177. public function niceShort($dateString = null, $timezone = null) {
  178. return $this->_engine->niceShort($dateString, $timezone);
  179. }
  180. /**
  181. * @see CakeTime::daysAsSql()
  182. *
  183. * @param string $begin Datetime string or Unix timestamp
  184. * @param string $end Datetime string or Unix timestamp
  185. * @param string $fieldName Name of database field to compare with
  186. * @param mixed $timezone User's timezone string or DateTimeZone object
  187. * @return string Partial SQL string.
  188. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  189. */
  190. public function daysAsSql($begin, $end, $fieldName, $timezone = null) {
  191. return $this->_engine->daysAsSql($begin, $end, $fieldName, $timezone);
  192. }
  193. /**
  194. * @see CakeTime::dayAsSql()
  195. *
  196. * @param string $dateString Datetime string or Unix timestamp
  197. * @param string $fieldName Name of database field to compare with
  198. * @param mixed $timezone User's timezone string or DateTimeZone object
  199. * @return string Partial SQL string.
  200. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  201. */
  202. public function dayAsSql($dateString, $fieldName, $timezone = null) {
  203. return $this->_engine->dayAsSql($dateString, $fieldName, $timezone);
  204. }
  205. /**
  206. * @see CakeTime::isToday()
  207. *
  208. * @param string $dateString Datetime string or Unix timestamp
  209. * @param mixed $timezone User's timezone string or DateTimeZone object
  210. * @return boolean True if datetime string is today
  211. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  212. */
  213. public function isToday($dateString, $timezone = null) {
  214. return $this->_engine->isToday($dateString, $timezone);
  215. }
  216. /**
  217. * @see CakeTime::isThisWeek()
  218. *
  219. * @param string $dateString
  220. * @param mixed $timezone User's timezone string or DateTimeZone object
  221. * @return boolean True if datetime string is within current week
  222. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  223. */
  224. public function isThisWeek($dateString, $timezone = null) {
  225. return $this->_engine->isThisWeek($dateString, $timezone);
  226. }
  227. /**
  228. * @see CakeTime::isThisMonth()
  229. *
  230. * @param string $dateString
  231. * @param mixed $timezone User's timezone string or DateTimeZone object
  232. * @return boolean True if datetime string is within current month
  233. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  234. */
  235. public function isThisMonth($dateString, $timezone = null) {
  236. return $this->_engine->isThisMonth($dateString, $timezone);
  237. }
  238. /**
  239. * @see CakeTime::isThisYear()
  240. *
  241. * @param string $dateString Datetime string or Unix timestamp
  242. * @param mixed $timezone User's timezone string or DateTimeZone object
  243. * @return boolean True if datetime string is within current year
  244. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  245. */
  246. public function isThisYear($dateString, $timezone = null) {
  247. return $this->_engine->isThisYear($dateString, $timezone);
  248. }
  249. /**
  250. * @see CakeTime::wasYesterday()
  251. *
  252. * @param string $dateString Datetime string or Unix timestamp
  253. * @param mixed $timezone User's timezone string or DateTimeZone object
  254. * @return boolean True if datetime string was yesterday
  255. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  256. *
  257. */
  258. public function wasYesterday($dateString, $timezone = null) {
  259. return $this->_engine->wasYesterday($dateString, $timezone);
  260. }
  261. /**
  262. * @see CakeTime::isTomorrow()
  263. *
  264. * @param string $dateString Datetime string or Unix timestamp
  265. * @param mixed $timezone User's timezone string or DateTimeZone object
  266. * @return boolean True if datetime string was yesterday
  267. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  268. */
  269. public function isTomorrow($dateString, $timezone = null) {
  270. return $this->_engine->isTomorrow($dateString, $timezone);
  271. }
  272. /**
  273. * @see CakeTime::toQuarter()
  274. *
  275. * @param string $dateString
  276. * @param boolean $range if true returns a range in Y-m-d format
  277. * @return mixed 1, 2, 3, or 4 quarter of year or array if $range true
  278. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  279. */
  280. public function toQuarter($dateString, $range = false) {
  281. return $this->_engine->toQuarter($dateString, $range);
  282. }
  283. /**
  284. * @see CakeTime::toUnix()
  285. *
  286. * @param string $dateString Datetime string to be represented as a Unix timestamp
  287. * @param mixed $timezone User's timezone string or DateTimeZone object
  288. * @return integer Unix timestamp
  289. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  290. */
  291. public function toUnix($dateString, $timezone = null) {
  292. return $this->_engine->toUnix($dateString, $timezone);
  293. }
  294. /**
  295. * @see CakeTime::toAtom()
  296. *
  297. * @param string $dateString Datetime string or Unix timestamp
  298. * @param mixed $timezone User's timezone string or DateTimeZone object
  299. * @return string Formatted date string
  300. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  301. */
  302. public function toAtom($dateString, $timezone = null) {
  303. return $this->_engine->toAtom($dateString, $timezone);
  304. }
  305. /**
  306. * @see CakeTime::toRSS()
  307. *
  308. * @param string $dateString Datetime string or Unix timestamp
  309. * @param mixed $timezone User's timezone string or DateTimeZone object
  310. * @return string Formatted date string
  311. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  312. */
  313. public function toRSS($dateString, $timezone = null) {
  314. return $this->_engine->toRSS($dateString, $timezone);
  315. }
  316. /**
  317. * @see CakeTime::timeAgoInWords()
  318. *
  319. * @param string $dateTime Datetime string or Unix timestamp
  320. * @param array $options Default format if timestamp is used in $dateString
  321. * @return string Relative time string.
  322. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  323. */
  324. public function timeAgoInWords($dateTime, $options = array()) {
  325. return $this->_engine->timeAgoInWords($dateTime, $options);
  326. }
  327. /**
  328. * @see CakeTime::wasWithinLast()
  329. *
  330. * @param mixed $timeInterval the numeric value with space then time type.
  331. * Example of valid types: 6 hours, 2 days, 1 minute.
  332. * @param mixed $dateString the datestring or unix timestamp to compare
  333. * @param mixed $timezone User's timezone string or DateTimeZone object
  334. * @return boolean
  335. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  336. */
  337. public function wasWithinLast($timeInterval, $dateString, $timezone = null) {
  338. return $this->_engine->wasWithinLast($timeInterval, $dateString, $timezone);
  339. }
  340. /**
  341. * @see CakeTime::gmt()
  342. *
  343. * @param string $string UNIX timestamp or a valid strtotime() date string
  344. * @return integer UNIX timestamp
  345. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  346. */
  347. public function gmt($string = null) {
  348. return $this->_engine->gmt($string);
  349. }
  350. /**
  351. * @see CakeTime::format()
  352. *
  353. * @param string $format date format string (or a DateTime string)
  354. * @param string $date Datetime string (or a date format string)
  355. * @param boolean $invalid flag to ignore results of fromString == false
  356. * @param mixed $timezone User's timezone string or DateTimeZone object
  357. * @return string Formatted date string
  358. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  359. */
  360. public function format($format, $date = null, $invalid = false, $timezone = null) {
  361. return $this->_engine->format($format, $date, $invalid, $timezone);
  362. }
  363. /**
  364. * @see CakeTime::i18nFormat()
  365. *
  366. * @param string $date Datetime string
  367. * @param string $format strftime format string.
  368. * @param boolean $invalid flag to ignore results of fromString == false
  369. * @param mixed $timezone User's timezone string or DateTimeZone object
  370. * @return string Formatted and translated date string
  371. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  372. */
  373. public function i18nFormat($date, $format = null, $invalid = false, $timezone = null) {
  374. return $this->_engine->i18nFormat($date, $format, $invalid, $timezone);
  375. }
  376. }