PageRenderTime 23ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/fakerphp/faker/src/Faker/Provider/DateTime.php

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