/core/Associates/FakerMaster/src/Faker/Provider/DateTime.php

https://gitlab.com/fiesta-framework/Documentation · PHP · 228 lines · 100 code · 28 blank · 100 comment · 3 complexity · 44bb629229f997c928ed5a1b8a920ec3 MD5 · raw file

  1. <?php
  2. namespace Faker\Provider;
  3. class DateTime extends \Faker\Provider\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 function getMaxTimestamp($max = 'now')
  7. {
  8. if (is_numeric($max)) {
  9. return (int) $max;
  10. }
  11. if ($max instanceof \DateTime) {
  12. return $max->getTimestamp();
  13. }
  14. return strtotime(empty($max) ? 'now' : $max);
  15. }
  16. /**
  17. * Get a timestamp between January 1, 1970 and now
  18. *
  19. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  20. *
  21. * @example 1061306726
  22. */
  23. public static function unixTime($max = 'now')
  24. {
  25. return mt_rand(0, static::getMaxTimestamp($max));
  26. }
  27. /**
  28. * Get a datetime object for a date between January 1, 1970 and now
  29. *
  30. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  31. * @example DateTime('2005-08-16 20:39:21')
  32. * @return \DateTime
  33. */
  34. public static function dateTime($max = 'now')
  35. {
  36. return new \DateTime('@' . static::unixTime($max));
  37. }
  38. /**
  39. * Get a datetime object for a date between January 1, 001 and now
  40. *
  41. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  42. * @example DateTime('1265-03-22 21:15:52')
  43. * @return \DateTime
  44. */
  45. public static function dateTimeAD($max = 'now')
  46. {
  47. return new \DateTime('@' . mt_rand(-62135597361, static::getMaxTimestamp($max)));
  48. }
  49. /**
  50. * get a date string formatted with ISO8601
  51. *
  52. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  53. * @example '2003-10-21T16:05:52+0000'
  54. */
  55. public static function iso8601($max = 'now')
  56. {
  57. return static::date(\DateTime::ISO8601, $max);
  58. }
  59. /**
  60. * Get a date string between January 1, 1970 and now
  61. *
  62. * @param string $format
  63. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  64. * @example '2008-11-27'
  65. */
  66. public static function date($format = 'Y-m-d', $max = 'now')
  67. {
  68. return static::dateTime($max)->format($format);
  69. }
  70. /**
  71. * Get a time string (24h format by default)
  72. *
  73. * @param string $format
  74. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  75. * @example '15:02:34'
  76. */
  77. public static function time($format = 'H:i:s', $max = 'now')
  78. {
  79. return static::dateTime($max)->format($format);
  80. }
  81. /**
  82. * Get a DateTime object based on a random date between two given dates.
  83. * Accepts date strings that can be recognized by strtotime().
  84. *
  85. * @param string $startDate Defaults to 30 years ago
  86. * @param string $endDate Defaults to "now"
  87. * @example DateTime('1999-02-02 11:42:52')
  88. * @return \DateTime
  89. */
  90. public static function dateTimeBetween($startDate = '-30 years', $endDate = 'now')
  91. {
  92. $startTimestamp = $startDate instanceof \DateTime ? $startDate->getTimestamp() : strtotime($startDate);
  93. $endTimestamp = static::getMaxTimestamp($endDate);
  94. if ($startTimestamp > $endTimestamp) {
  95. throw new \InvalidArgumentException('Start date must be anterior to end date.');
  96. }
  97. $timestamp = mt_rand($startTimestamp, $endTimestamp);
  98. $ts = new \DateTime('@' . $timestamp);
  99. $ts->setTimezone(new \DateTimeZone(date_default_timezone_get()));
  100. return $ts;
  101. }
  102. /**
  103. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  104. * @example DateTime('1964-04-04 11:02:02')
  105. * @return \DateTime
  106. */
  107. public static function dateTimeThisCentury($max = 'now')
  108. {
  109. return static::dateTimeBetween('-100 year', $max);
  110. }
  111. /**
  112. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  113. * @example DateTime('2010-03-10 05:18:58')
  114. * @return \DateTime
  115. */
  116. public static function dateTimeThisDecade($max = 'now')
  117. {
  118. return static::dateTimeBetween('-10 year', $max);
  119. }
  120. /**
  121. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  122. * @example DateTime('2011-09-19 09:24:37')
  123. * @return \DateTime
  124. */
  125. public static function dateTimeThisYear($max = 'now')
  126. {
  127. return static::dateTimeBetween('-1 year', $max);
  128. }
  129. /**
  130. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  131. * @example DateTime('2011-10-05 12:51:46')
  132. * @return \DateTime
  133. */
  134. public static function dateTimeThisMonth($max = 'now')
  135. {
  136. return static::dateTimeBetween('-1 month', $max);
  137. }
  138. /**
  139. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  140. * @example 'am'
  141. */
  142. public static function amPm($max = 'now')
  143. {
  144. return static::dateTime($max)->format('a');
  145. }
  146. /**
  147. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  148. * @example '22'
  149. */
  150. public static function dayOfMonth($max = 'now')
  151. {
  152. return static::dateTime($max)->format('d');
  153. }
  154. /**
  155. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  156. * @example 'Tuesday'
  157. */
  158. public static function dayOfWeek($max = 'now')
  159. {
  160. return static::dateTime($max)->format('l');
  161. }
  162. /**
  163. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  164. * @example '7'
  165. */
  166. public static function month($max = 'now')
  167. {
  168. return static::dateTime($max)->format('m');
  169. }
  170. /**
  171. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  172. * @example 'September'
  173. */
  174. public static function monthName($max = 'now')
  175. {
  176. return static::dateTime($max)->format('F');
  177. }
  178. /**
  179. * @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
  180. * @example 1673
  181. */
  182. public static function year($max = 'now')
  183. {
  184. return static::dateTime($max)->format('Y');
  185. }
  186. /**
  187. * @example 'XVII'
  188. */
  189. public static function century()
  190. {
  191. return static::randomElement(static::$century);
  192. }
  193. /**
  194. * @example 'Europe/Paris'
  195. */
  196. public static function timezone()
  197. {
  198. return static::randomElement(\DateTimeZone::listIdentifiers());
  199. }
  200. }