PageRenderTime 57ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Faker/Provider/DateTime.php

http://github.com/fzaninotto/Faker
PHP | 340 lines | 139 code | 35 blank | 166 comment | 3 complexity | 889e68a7919ffbe4f569b276620b9318 MD5 | raw file
  1. <?php
  2. namespace Faker\Provider;
  3. class DateTime extends Base
  4. {
  5. protected static $century = array('I','II','III','IV','V','VI','VII','VIII','IX','X','XI','XII','XIII','XIV','XV','XVI','XVII','XVIII','XIX','XX','XXI');
  6. protected static $defaultTimezone = null;
  7. /**
  8. * @param \DateTime|string|float|int $max
  9. * @return int|false
  10. */
  11. protected static function getMaxTimestamp($max = 'now')
  12. {
  13. if (is_numeric($max)) {
  14. return (int) $max;
  15. }
  16. if ($max instanceof \DateTime) {
  17. return $max->getTimestamp();
  18. }
  19. return strtotime(empty($max) ? 'now' : $max);
  20. }
  21. /**
  22. * Get a timestamp between January 1, 1970 and now
  23. *
  24. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  25. * @return int
  26. *
  27. * @example 1061306726
  28. */
  29. public static function unixTime($max = 'now')
  30. {
  31. return mt_rand(0, static::getMaxTimestamp($max));
  32. }
  33. /**
  34. * Get a datetime object for a date between January 1, 1970 and now
  35. *
  36. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  37. * @param string $timezone time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of `date_default_timezone_get`
  38. * @example DateTime('2005-08-16 20:39:21')
  39. * @return \DateTime
  40. * @see http://php.net/manual/en/timezones.php
  41. * @see http://php.net/manual/en/function.date-default-timezone-get.php
  42. */
  43. public static function dateTime($max = 'now', $timezone = null)
  44. {
  45. return static::setTimezone(
  46. new \DateTime('@' . static::unixTime($max)),
  47. $timezone
  48. );
  49. }
  50. /**
  51. * Get a datetime object for a date between January 1, 001 and now
  52. *
  53. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  54. * @param string|null $timezone time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of `date_default_timezone_get`
  55. * @example DateTime('1265-03-22 21:15:52')
  56. * @return \DateTime
  57. * @see http://php.net/manual/en/timezones.php
  58. * @see http://php.net/manual/en/function.date-default-timezone-get.php
  59. */
  60. public static function dateTimeAD($max = 'now', $timezone = null)
  61. {
  62. $min = (PHP_INT_SIZE>4 ? -62135597361 : -PHP_INT_MAX);
  63. return static::setTimezone(
  64. new \DateTime('@' . mt_rand($min, static::getMaxTimestamp($max))),
  65. $timezone
  66. );
  67. }
  68. /**
  69. * get a date string formatted with ISO8601
  70. *
  71. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  72. * @return string
  73. * @example '2003-10-21T16:05:52+0000'
  74. */
  75. public static function iso8601($max = 'now')
  76. {
  77. return static::date(\DateTime::ISO8601, $max);
  78. }
  79. /**
  80. * Get a date string between January 1, 1970 and now
  81. *
  82. * @param string $format
  83. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  84. * @return string
  85. * @example '2008-11-27'
  86. */
  87. public static function date($format = 'Y-m-d', $max = 'now')
  88. {
  89. return static::dateTime($max)->format($format);
  90. }
  91. /**
  92. * Get a time string (24h format by default)
  93. *
  94. * @param string $format
  95. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  96. * @return string
  97. * @example '15:02:34'
  98. */
  99. public static function time($format = 'H:i:s', $max = 'now')
  100. {
  101. return static::dateTime($max)->format($format);
  102. }
  103. /**
  104. * Get a DateTime object based on a random date between two given dates.
  105. * Accepts date strings that can be recognized by strtotime().
  106. *
  107. * @param \DateTime|string $startDate Defaults to 30 years ago
  108. * @param \DateTime|string $endDate Defaults to "now"
  109. * @param string|null $timezone time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of `date_default_timezone_get`
  110. * @example DateTime('1999-02-02 11:42:52')
  111. * @return \DateTime
  112. * @see http://php.net/manual/en/timezones.php
  113. * @see http://php.net/manual/en/function.date-default-timezone-get.php
  114. */
  115. public static function dateTimeBetween($startDate = '-30 years', $endDate = 'now', $timezone = null)
  116. {
  117. $startTimestamp = $startDate instanceof \DateTime ? $startDate->getTimestamp() : strtotime($startDate);
  118. $endTimestamp = static::getMaxTimestamp($endDate);
  119. if ($startTimestamp > $endTimestamp) {
  120. throw new \InvalidArgumentException('Start date must be anterior to end date.');
  121. }
  122. $timestamp = mt_rand($startTimestamp, $endTimestamp);
  123. return static::setTimezone(
  124. new \DateTime('@' . $timestamp),
  125. $timezone
  126. );
  127. }
  128. /**
  129. * Get a DateTime object based on a random date between one given date and
  130. * an interval
  131. * Accepts date string that can be recognized by strtotime().
  132. *
  133. * @param \DateTime|string $date Defaults to 30 years ago
  134. * @param string $interval Defaults to 5 days after
  135. * @param string|null $timezone time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of `date_default_timezone_get`
  136. * @example dateTimeInInterval('1999-02-02 11:42:52', '+ 5 days')
  137. * @return \DateTime
  138. * @see http://php.net/manual/en/timezones.php
  139. * @see http://php.net/manual/en/function.date-default-timezone-get.php
  140. */
  141. public static function dateTimeInInterval($date = '-30 years', $interval = '+5 days', $timezone = null)
  142. {
  143. $intervalObject = \DateInterval::createFromDateString($interval);
  144. $datetime = $date instanceof \DateTime ? $date : new \DateTime($date);
  145. $otherDatetime = clone $datetime;
  146. $otherDatetime->add($intervalObject);
  147. $begin = $datetime > $otherDatetime ? $otherDatetime : $datetime;
  148. $end = $datetime===$begin ? $otherDatetime : $datetime;
  149. return static::dateTimeBetween(
  150. $begin,
  151. $end,
  152. $timezone
  153. );
  154. }
  155. /**
  156. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  157. * @param string|null $timezone time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of `date_default_timezone_get`
  158. * @example DateTime('1964-04-04 11:02:02')
  159. * @return \DateTime
  160. */
  161. public static function dateTimeThisCentury($max = 'now', $timezone = null)
  162. {
  163. return static::dateTimeBetween('-100 year', $max, $timezone);
  164. }
  165. /**
  166. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  167. * @param string|null $timezone time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of `date_default_timezone_get`
  168. * @example DateTime('2010-03-10 05:18:58')
  169. * @return \DateTime
  170. */
  171. public static function dateTimeThisDecade($max = 'now', $timezone = null)
  172. {
  173. return static::dateTimeBetween('-10 year', $max, $timezone);
  174. }
  175. /**
  176. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  177. * @param string|null $timezone time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of `date_default_timezone_get`
  178. * @example DateTime('2011-09-19 09:24:37')
  179. * @return \DateTime
  180. */
  181. public static function dateTimeThisYear($max = 'now', $timezone = null)
  182. {
  183. return static::dateTimeBetween('-1 year', $max, $timezone);
  184. }
  185. /**
  186. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  187. * @param string|null $timezone time zone in which the date time should be set, default to DateTime::$defaultTimezone, if set, otherwise the result of `date_default_timezone_get`
  188. * @example DateTime('2011-10-05 12:51:46')
  189. * @return \DateTime
  190. */
  191. public static function dateTimeThisMonth($max = 'now', $timezone = null)
  192. {
  193. return static::dateTimeBetween('-1 month', $max, $timezone);
  194. }
  195. /**
  196. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  197. * @return string
  198. * @example 'am'
  199. */
  200. public static function amPm($max = 'now')
  201. {
  202. return static::dateTime($max)->format('a');
  203. }
  204. /**
  205. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  206. * @return string
  207. * @example '22'
  208. */
  209. public static function dayOfMonth($max = 'now')
  210. {
  211. return static::dateTime($max)->format('d');
  212. }
  213. /**
  214. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  215. * @return string
  216. * @example 'Tuesday'
  217. */
  218. public static function dayOfWeek($max = 'now')
  219. {
  220. return static::dateTime($max)->format('l');
  221. }
  222. /**
  223. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  224. * @return string
  225. * @example '7'
  226. */
  227. public static function month($max = 'now')
  228. {
  229. return static::dateTime($max)->format('m');
  230. }
  231. /**
  232. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  233. * @return string
  234. * @example 'September'
  235. */
  236. public static function monthName($max = 'now')
  237. {
  238. return static::dateTime($max)->format('F');
  239. }
  240. /**
  241. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  242. * @return string
  243. * @example '1673'
  244. */
  245. public static function year($max = 'now')
  246. {
  247. return static::dateTime($max)->format('Y');
  248. }
  249. /**
  250. * @return string
  251. * @example 'XVII'
  252. */
  253. public static function century()
  254. {
  255. return static::randomElement(static::$century);
  256. }
  257. /**
  258. * @return string
  259. * @example 'Europe/Paris'
  260. */
  261. public static function timezone()
  262. {
  263. return static::randomElement(\DateTimeZone::listIdentifiers());
  264. }
  265. /**
  266. * Internal method to set the time zone on a DateTime.
  267. *
  268. * @param \DateTime $dt
  269. * @param string|null $timezone
  270. *
  271. * @return \DateTime
  272. */
  273. private static function setTimezone(\DateTime $dt, $timezone)
  274. {
  275. return $dt->setTimezone(new \DateTimeZone(static::resolveTimezone($timezone)));
  276. }
  277. /**
  278. * Sets default time zone.
  279. *
  280. * @param string $timezone
  281. *
  282. * @return void
  283. */
  284. public static function setDefaultTimezone($timezone = null)
  285. {
  286. static::$defaultTimezone = $timezone;
  287. }
  288. /**
  289. * Gets default time zone.
  290. *
  291. * @return string|null
  292. */
  293. public static function getDefaultTimezone()
  294. {
  295. return static::$defaultTimezone;
  296. }
  297. /**
  298. * @param string|null $timezone
  299. * @return null|string
  300. */
  301. private static function resolveTimezone($timezone)
  302. {
  303. return ((null === $timezone) ? ((null === static::$defaultTimezone) ? date_default_timezone_get() : static::$defaultTimezone) : $timezone);
  304. }
  305. }