PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/en/core-utility-libraries/time.rst

https://github.com/steinkel/docs
ReStructuredText | 440 lines | 305 code | 135 blank | 0 comment | 0 complexity | ac93330d03f308cc0a3c2291ab9df303 MD5 | raw file
  1. CakeTime
  2. ########
  3. .. php:class:: CakeTime()
  4. If you need :php:class:`TimeHelper` functionalities outside of a ``View``,
  5. use the ``CakeTime`` class::
  6. class UsersController extends AppController {
  7. public $components = array('Auth');
  8. public function afterLogin() {
  9. App::uses('CakeTime', 'Utility');
  10. if (CakeTime::isToday($this->Auth->user('date_of_birth']))) {
  11. // greet user with a happy birthday message
  12. $this->Session->setFlash(__('Happy birthday you...'));
  13. }
  14. }
  15. }
  16. .. versionadded:: 2.1
  17. ``CakeTime`` has been factored out from :php:class:`TimeHelper`.
  18. .. start-caketime
  19. Formatting
  20. ==========
  21. .. php:method:: convert($serverTime, $timezone = NULL)
  22. :rtype: integer
  23. Converts given time (in server's time zone) to user's local
  24. time, given his/her timezone. ::
  25. // called via TimeHelper
  26. echo $this->Time->convert(time(), 'Asia/Jakarta');
  27. // 1321038036
  28. // called as CakeTime
  29. App::uses('CakeTime', 'Utility');
  30. echo CakeTime::convert(time(), new DateTimeZone('Asia/Jakarta'));
  31. .. versionchanged:: 2.2
  32. ``$timezone`` parameter replaces ``$userOffset`` parameter used in 2.1 and below.
  33. .. php:method:: convertSpecifiers($format, $time = NULL)
  34. :rtype: string
  35. Converts a string representing the format for the function
  36. strftime and returns a Windows safe and i18n aware format.
  37. .. php:method:: dayAsSql($dateString, $field_name, $timezone = NULL)
  38. :rtype: string
  39. Creates a string in the same format as daysAsSql but
  40. only needs a single date object::
  41. // called via TimeHelper
  42. echo $this->Time->dayAsSql('Aug 22, 2011', 'modified');
  43. // (modified >= '2011-08-22 00:00:00') AND
  44. // (modified <= '2011-08-22 23:59:59')
  45. // called as CakeTime
  46. App::uses('CakeTime', 'Utility');
  47. echo CakeTime::dayAsSql('Aug 22, 2011', 'modified');
  48. .. versionchanged:: 2.2
  49. ``$timezone`` parameter replaces ``$userOffset`` parameter used in 2.1 and below.
  50. .. versionadded:: 2.2
  51. ``$dateString`` parameter now also accepts a DateTime object.
  52. .. php:method:: daysAsSql($begin, $end, $fieldName, $timezone = NULL)
  53. :rtype: string
  54. Returns a string in the format "($field\_name >=
  55. '2008-01-21 00:00:00') AND ($field\_name <= '2008-01-25
  56. 23:59:59')". This is handy if you need to search for records
  57. between two dates inclusively::
  58. // called via TimeHelper
  59. echo $this->Time->daysAsSql('Aug 22, 2011', 'Aug 25, 2011', 'created');
  60. // (created >= '2011-08-22 00:00:00') AND
  61. // (created <= '2011-08-25 23:59:59')
  62. // called as CakeTime
  63. App::uses('CakeTime', 'Utility');
  64. echo CakeTime::daysAsSql('Aug 22, 2011', 'Aug 25, 2011', 'created');
  65. .. versionchanged:: 2.2
  66. ``$timezone`` parameter replaces ``$userOffset`` parameter used in 2.1 and below.
  67. .. versionadded:: 2.2
  68. ``$dateString`` parameter now also accepts a DateTime object.
  69. .. php:method:: format($date, $format = NULL, $default = false, $timezone = NULL)
  70. :rtype: string
  71. Will return a string formatted to the given format using the
  72. `PHP strftime() formatting options <https://secure.php.net/manual/en/function.strftime.php>`_::
  73. // called via TimeHelper
  74. echo $this->Time->format('2011-08-22 11:53:00', '%B %e, %Y %H:%M %p');
  75. // August 22, 2011 11:53 AM
  76. echo $this->Time->format('+2 days', '%c');
  77. // 2 days from now formatted as Sun, 13 Nov 2011 03:36:10 AM EET
  78. // called as CakeTime
  79. App::uses('CakeTime', 'Utility');
  80. echo CakeTime::format('2011-08-22 11:53:00', '%B %e, %Y %H:%M %p');
  81. echo CakeTime::format('+2 days', '%c');
  82. You can also provide the date/time as the first argument. When doing this
  83. you should use ``strftime`` compatible formatting. This call signature
  84. allows you to leverage locale aware date formatting which is not possible
  85. using ``date()`` compatible formatting::
  86. // called via TimeHelper
  87. echo $this->Time->format('2012-01-13', '%d-%m-%Y', 'invalid');
  88. // called as CakeTime
  89. App::uses('CakeTime', 'Utility');
  90. echo CakeTime::format('2011-08-22', '%d-%m-%Y');
  91. .. versionchanged:: 2.2
  92. ``$format`` and ``$date`` parameters are in opposite order as used in 2.1 and below.
  93. ``$timezone`` parameter replaces ``$userOffset`` parameter used in 2.1 and below.
  94. ``$default`` parameter replaces ``$invalid`` parameter used in 2.1 and below.
  95. .. versionadded:: 2.2
  96. ``$date`` parameter now also accepts a DateTime object.
  97. .. php:method:: fromString($dateString, $timezone = NULL)
  98. :rtype: string
  99. Takes a string and uses `strtotime <http://us.php.net/manual/en/function.date.php>`_
  100. to convert it into a date integer::
  101. // called via TimeHelper
  102. echo $this->Time->fromString('Aug 22, 2011');
  103. // 1313971200
  104. echo $this->Time->fromString('+1 days');
  105. // 1321074066 (+1 day from current date)
  106. // called as CakeTime
  107. App::uses('CakeTime', 'Utility');
  108. echo CakeTime::fromString('Aug 22, 2011');
  109. echo CakeTime::fromString('+1 days');
  110. .. versionchanged:: 2.2
  111. ``$timezone`` parameter replaces ``$userOffset`` parameter used in 2.1 and below.
  112. .. versionadded:: 2.2
  113. ``$dateString`` parameter now also accepts a DateTime object.
  114. .. php:method:: gmt($dateString = NULL)
  115. :rtype: integer
  116. Will return the date as an integer set to Greenwich Mean Time (GMT). ::
  117. // called via TimeHelper
  118. echo $this->Time->gmt('Aug 22, 2011');
  119. // 1313971200
  120. // called as CakeTime
  121. App::uses('CakeTime', 'Utility');
  122. echo CakeTime::gmt('Aug 22, 2011');
  123. .. php:method:: i18nFormat($date, $format = NULL, $invalid = false, $timezone = NULL)
  124. :rtype: string
  125. Returns a formatted date string, given either a UNIX timestamp or a
  126. valid strtotime() date string. It take in account the default date
  127. format for the current language if a LC_TIME file is used. For more info
  128. about LC_TIME file check :ref:`here <lc-time>`.
  129. .. versionchanged:: 2.2
  130. ``$timezone`` parameter replaces ``$userOffset`` parameter used in 2.1 and below.
  131. .. php:method:: nice($dateString = NULL, $timezone = NULL, $format = null)
  132. :rtype: string
  133. Takes a date string and outputs it in the format "Tue, Jan
  134. 1st 2008, 19:25" or as per optional ``$format`` param passed::
  135. // called via TimeHelper
  136. echo $this->Time->nice('2011-08-22 11:53:00');
  137. // Mon, Aug 22nd 2011, 11:53
  138. // called as CakeTime
  139. App::uses('CakeTime', 'Utility');
  140. echo CakeTime::nice('2011-08-22 11:53:00');
  141. .. php:method:: niceShort($dateString = NULL, $timezone = NULL)
  142. :rtype: string
  143. Takes a date string and outputs it in the format "Jan
  144. 1st 2008, 19:25". If the date object is today, the format will be
  145. "Today, 19:25". If the date object is yesterday, the format will be
  146. "Yesterday, 19:25"::
  147. // called via TimeHelper
  148. echo $this->Time->niceShort('2011-08-22 11:53:00');
  149. // Aug 22nd, 11:53
  150. // called as CakeTime
  151. App::uses('CakeTime', 'Utility');
  152. echo CakeTime::niceShort('2011-08-22 11:53:00');
  153. .. versionchanged:: 2.2
  154. ``$timezone`` parameter replaces ``$userOffset`` parameter used in 2.1 and below.
  155. .. versionadded:: 2.2
  156. ``$dateString`` parameter now also accepts a DateTime object.
  157. .. php:method:: serverOffset()
  158. :rtype: integer
  159. Returns server's offset from GMT in seconds.
  160. .. php:method:: timeAgoInWords($dateString, $options = array())
  161. :rtype: string
  162. Will take a datetime string (anything that is
  163. parsable by PHP's strtotime() function or MySQL's datetime format)
  164. and convert it into a friendly word format like, "3 weeks, 3 days
  165. ago"::
  166. // called via TimeHelper
  167. echo $this->Time->timeAgoInWords('Aug 22, 2011');
  168. // on 22/8/11
  169. // on August 22nd, 2011
  170. echo $this->Time->timeAgoInWords(
  171. 'Aug 22, 2011',
  172. array('format' => 'F jS, Y')
  173. );
  174. // called as CakeTime
  175. App::uses('CakeTime', 'Utility');
  176. echo CakeTime::timeAgoInWords('Aug 22, 2011');
  177. echo CakeTime::timeAgoInWords(
  178. 'Aug 22, 2011',
  179. array('format' => 'F jS, Y')
  180. );
  181. Use the 'end' option to determine the cutoff point to no longer will use words; default '+1 month'::
  182. // called via TimeHelper
  183. echo $this->Time->timeAgoInWords(
  184. 'Aug 22, 2011',
  185. array('format' => 'F jS, Y', 'end' => '+1 year')
  186. );
  187. // On Nov 10th, 2011 it would display: 2 months, 2 weeks, 6 days ago
  188. // called as CakeTime
  189. App::uses('CakeTime', 'Utility');
  190. echo CakeTime::timeAgoInWords(
  191. 'Aug 22, 2011',
  192. array('format' => 'F jS, Y', 'end' => '+1 year')
  193. );
  194. Use the 'accuracy' option to determine how precise the output should be.
  195. You can use this to limit the output::
  196. // If $timestamp is 1 month, 1 week, 5 days and 6 hours ago
  197. echo CakeTime::timeAgoInWords($timestamp, array(
  198. 'accuracy' => array('month' => 'month'),
  199. 'end' => '1 year'
  200. ));
  201. // Outputs '1 month ago'
  202. .. versionchanged:: 2.2
  203. The ``accuracy`` option was added.
  204. .. versionadded:: 2.2
  205. ``$dateString`` parameter now also accepts a DateTime object.
  206. .. php:method:: toAtom($dateString, $timezone = NULL)
  207. :rtype: string
  208. Will return a date string in the Atom format "2008-01-12T00:00:00Z"
  209. .. versionchanged:: 2.2
  210. ``$timezone`` parameter replaces ``$userOffset`` parameter used in 2.1 and below.
  211. .. versionadded:: 2.2
  212. ``$dateString`` parameter now also accepts a DateTime object.
  213. .. php:method:: toQuarter($dateString, $range = false)
  214. :rtype: mixed
  215. Will return 1, 2, 3 or 4 depending on what quarter of
  216. the year the date falls in. If range is set to true, a two element
  217. array will be returned with start and end dates in the format
  218. "2008-03-31"::
  219. // called via TimeHelper
  220. echo $this->Time->toQuarter('Aug 22, 2011');
  221. // Would print 3
  222. $arr = $this->Time->toQuarter('Aug 22, 2011', true);
  223. /*
  224. Array
  225. (
  226. [0] => 2011-07-01
  227. [1] => 2011-09-30
  228. )
  229. */
  230. // called as CakeTime
  231. App::uses('CakeTime', 'Utility');
  232. echo CakeTime::toQuarter('Aug 22, 2011');
  233. $arr = CakeTime::toQuarter('Aug 22, 2011', true);
  234. .. versionadded:: 2.2
  235. ``$dateString`` parameter now also accepts a DateTime object.
  236. .. versionadded:: 2.4
  237. The new option parameters ``relativeString`` (defaults to ``%s ago``) and
  238. ``absoluteString`` (defaults to ``on %s``) to allow customization of the resulting
  239. output string are now available.
  240. .. php:method:: toRSS($dateString, $timezone = NULL)
  241. :rtype: string
  242. Will return a date string in the RSS format "Sat, 12 Jan 2008
  243. 00:00:00 -0500"
  244. .. versionchanged:: 2.2
  245. ``$timezone`` parameter replaces ``$userOffset`` parameter used in 2.1 and below.
  246. .. versionadded:: 2.2
  247. ``$dateString`` parameter now also accepts a DateTime object.
  248. .. php:method:: toUnix($dateString, $timezone = NULL)
  249. :rtype: integer
  250. A wrapper for fromString.
  251. .. versionchanged:: 2.2
  252. ``$timezone`` parameter replaces ``$userOffset`` parameter used in 2.1 and below.
  253. .. versionadded:: 2.2
  254. ``$dateString`` parameter now also accepts a DateTime object.
  255. .. php:method:: toServer($dateString, $timezone = NULL, $format = 'Y-m-d H:i:s')
  256. :rtype: mixed
  257. .. versionadded:: 2.2
  258. Returns a formatted date in server's timezone.
  259. .. php:method:: timezone($timezone = NULL)
  260. :rtype: DateTimeZone
  261. .. versionadded:: 2.2
  262. Returns a timezone object from a string or the user's timezone object. If the function is called
  263. without a parameter it tries to get timezone from 'Config.timezone' configuration variable.
  264. .. php:method:: listTimezones($filter = null, $country = null, $options = array())
  265. :rtype: array
  266. .. versionadded:: 2.2
  267. Returns a list of timezone identifiers.
  268. .. versionchanged:: 2.8
  269. ``$options`` now accepts array with ``group``, ``abbr``, ``before``, and ``after`` keys.
  270. Specify ``abbr => true`` will append the timezone abbreviation in the ``<option>`` text.
  271. Testing Time
  272. ============
  273. .. php:method:: isToday($dateString, $timezone = NULL)
  274. .. php:method:: isThisWeek($dateString, $timezone = NULL)
  275. .. php:method:: isThisMonth($dateString, $timezone = NULL)
  276. .. php:method:: isThisYear($dateString, $timezone = NULL)
  277. .. php:method:: wasYesterday($dateString, $timezone = NULL)
  278. .. php:method:: isTomorrow($dateString, $timezone = NULL)
  279. .. php:method:: isFuture($dateString, $timezone = NULL)
  280. .. versionadded:: 2.4
  281. .. php:method:: isPast($dateString, $timezone = NULL)
  282. .. versionadded:: 2.4
  283. .. php:method:: wasWithinLast($timeInterval, $dateString, $timezone = NULL)
  284. .. versionchanged:: 2.2
  285. ``$timezone`` parameter replaces ``$userOffset`` parameter used in 2.1 and below.
  286. .. versionadded:: 2.2
  287. ``$dateString`` parameter now also accepts a DateTime object.
  288. All of the above functions return true or false when passed a date
  289. string. ``wasWithinLast`` takes an additional ``$timeInterval``
  290. option::
  291. // called via TimeHelper
  292. $this->Time->wasWithinLast($timeInterval, $dateString);
  293. // called as CakeTime
  294. App::uses('CakeTime', 'Utility');
  295. CakeTime::wasWithinLast($timeInterval, $dateString);
  296. ``wasWithinLast`` takes a time interval which is a string in the
  297. format "3 months" and accepts a time interval of seconds, minutes,
  298. hours, days, weeks, months and years (plural and not). If a time
  299. interval is not recognized (for example, if it is mistyped) then it
  300. will default to days.
  301. .. end-caketime
  302. .. meta::
  303. :title lang=en: CakeTime
  304. :description lang=en: CakeTime class helps you format time and test time.
  305. :keywords lang=en: time,format time,timezone,unix epoch,time strings,time zone offset,utc,gmt