DateTimeExtension.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace Faker\Extension;
  3. /**
  4. * FakerPHP extension for Date-related randomization.
  5. *
  6. * Functions accepting a date string use the `strtotime()` function internally.
  7. *
  8. * @experimental
  9. *
  10. * @since 1.20.0
  11. */
  12. interface DateTimeExtension
  13. {
  14. /**
  15. * Get a DateTime object between January 1, 1970, and `$until` (defaults to "now").
  16. *
  17. * @param \DateTime|int|string $until maximum timestamp, defaults to "now"
  18. * @param string|null $timezone zone timezone for generated date, fallback to `DateTime::$defaultTimezone` and `date_default_timezone_get()`.
  19. *
  20. * @see \DateTimeZone
  21. * @see http://php.net/manual/en/timezones.php
  22. * @see http://php.net/manual/en/function.date-default-timezone-get.php
  23. *
  24. * @example DateTime('2005-08-16 20:39:21')
  25. */
  26. public function dateTime($until = 'now', string $timezone = null): \DateTime;
  27. /**
  28. * Get a DateTime object for a date between January 1, 0001, and now.
  29. *
  30. * @param \DateTime|int|string $until maximum timestamp, defaults to "now"
  31. * @param string|null $timezone zone timezone for generated date, fallback to `DateTime::$defaultTimezone` and `date_default_timezone_get()`.
  32. *
  33. * @example DateTime('1265-03-22 21:15:52')
  34. *
  35. * @see http://php.net/manual/en/timezones.php
  36. * @see http://php.net/manual/en/function.date-default-timezone-get.php
  37. */
  38. public function dateTimeAD($until = 'now', string $timezone = null): \DateTime;
  39. /**
  40. * Get a DateTime object a random date between `$from` and `$until`.
  41. * Accepts date strings that can be recognized by `strtotime()`.
  42. *
  43. * @param \DateTime|string $from defaults to 30 years ago
  44. * @param \DateTime|int|string $until maximum timestamp, defaults to "now"
  45. * @param string|null $timezone zone timezone for generated date, fallback to `DateTime::$defaultTimezone` and `date_default_timezone_get()`.
  46. *
  47. * @see \DateTimeZone
  48. * @see http://php.net/manual/en/timezones.php
  49. * @see http://php.net/manual/en/function.date-default-timezone-get.php
  50. */
  51. public function dateTimeBetween($from = '-30 years', $until = 'now', string $timezone = null): \DateTime;
  52. /**
  53. * Get a DateTime object based on a random date between `$from` and an interval.
  54. * Accepts date string that can be recognized by `strtotime()`.
  55. *
  56. * @param \DateTime|int|string $from defaults to 30 years ago
  57. * @param string $interval defaults to 5 days after
  58. * @param string|null $timezone zone timezone for generated date, fallback to `DateTime::$defaultTimezone` and `date_default_timezone_get()`.
  59. *
  60. * @see \DateTimeZone
  61. * @see http://php.net/manual/en/timezones.php
  62. * @see http://php.net/manual/en/function.date-default-timezone-get.php
  63. */
  64. public function dateTimeInInterval($from = '-30 years', string $interval = '+5 days', string $timezone = null): \DateTime;
  65. /**
  66. * Get a date time object somewhere inside the current week.
  67. *
  68. * @param \DateTime|int|string $until maximum timestamp, defaults to "now"
  69. * @param string|null $timezone zone timezone for generated date, fallback to `DateTime::$defaultTimezone` and `date_default_timezone_get()`.
  70. *
  71. * @see \DateTimeZone
  72. * @see http://php.net/manual/en/timezones.php
  73. * @see http://php.net/manual/en/function.date-default-timezone-get.php
  74. */
  75. public function dateTimeThisWeek($until = 'now', string $timezone = null): \DateTime;
  76. /**
  77. * Get a date time object somewhere inside the current month.
  78. *
  79. * @param \DateTime|int|string $until maximum timestamp, defaults to "now"
  80. * @param string|null $timezone timezone for generated date, fallback to `DateTime::$defaultTimezone` and `date_default_timezone_get()`.
  81. *
  82. * @see \DateTimeZone
  83. * @see http://php.net/manual/en/timezones.php
  84. * @see http://php.net/manual/en/function.date-default-timezone-get.php
  85. */
  86. public function dateTimeThisMonth($until = 'now', string $timezone = null): \DateTime;
  87. /**
  88. * Get a date time object somewhere inside the current year.
  89. *
  90. * @param \DateTime|int|string $until maximum timestamp, defaults to "now"
  91. * @param string|null $timezone timezone for generated date, fallback to `DateTime::$defaultTimezone` and `date_default_timezone_get()`.
  92. *
  93. * @see \DateTimeZone
  94. * @see http://php.net/manual/en/timezones.php
  95. * @see http://php.net/manual/en/function.date-default-timezone-get.php
  96. */
  97. public function dateTimeThisYear($until = 'now', string $timezone = null): \DateTime;
  98. /**
  99. * Get a date time object somewhere inside the current decade.
  100. *
  101. * @param \DateTime|int|string $until maximum timestamp, defaults to "now"
  102. * @param string|null $timezone timezone for generated date, fallback to `DateTime::$defaultTimezone` and `date_default_timezone_get()`.
  103. *
  104. * @see \DateTimeZone
  105. * @see http://php.net/manual/en/timezones.php
  106. * @see http://php.net/manual/en/function.date-default-timezone-get.php
  107. */
  108. public function dateTimeThisDecade($until = 'now', string $timezone = null): \DateTime;
  109. /**
  110. * Get a date time object somewhere inside the current century.
  111. *
  112. * @param \DateTime|int|string $until maximum timestamp, defaults to "now"
  113. * @param string|null $timezone timezone for generated date, fallback to `DateTime::$defaultTimezone` and `date_default_timezone_get()`.
  114. *
  115. * @see \DateTimeZone
  116. * @see http://php.net/manual/en/timezones.php
  117. * @see http://php.net/manual/en/function.date-default-timezone-get.php
  118. */
  119. public function dateTimeThisCentury($until = 'now', string $timezone = null): \DateTime;
  120. /**
  121. * Get a date string between January 1, 1970, and `$until`.
  122. *
  123. * @param string $format DateTime format
  124. * @param \DateTime|int|string $until maximum timestamp, defaults to "now"
  125. *
  126. * @see https://www.php.net/manual/en/datetime.format.php
  127. */
  128. public function date(string $format = 'Y-m-d', $until = 'now'): string;
  129. /**
  130. * Get a time string (24h format by default).
  131. *
  132. * @param string $format DateTime format
  133. * @param \DateTime|int|string $until maximum timestamp, defaults to "now"
  134. *
  135. * @see https://www.php.net/manual/en/datetime.format.php
  136. */
  137. public function time(string $format = 'H:i:s', $until = 'now'): string;
  138. /**
  139. * Get a UNIX (POSIX-compatible) timestamp between January 1, 1970, and `$until`.
  140. *
  141. * @param \DateTime|int|string $until maximum timestamp, defaults to "now"
  142. */
  143. public function unixTime($until = 'now'): int;
  144. /**
  145. * Get a date string according to the ISO-8601 standard.
  146. *
  147. * @param \DateTime|int|string $until maximum timestamp, defaults to "now"
  148. */
  149. public function iso8601($until = 'now'): string;
  150. /**
  151. * Get a string containing either "am" or "pm".
  152. *
  153. * @param \DateTime|int|string $until maximum timestamp, defaults to "now"
  154. *
  155. * @example 'am'
  156. */
  157. public function amPm($until = 'now'): string;
  158. /**
  159. * Get a localized random day of the month.
  160. *
  161. * @param \DateTime|int|string $until maximum timestamp, defaults to "now"
  162. *
  163. * @example '16'
  164. */
  165. public function dayOfMonth($until = 'now'): string;
  166. /**
  167. * Get a localized random day of the week.
  168. *
  169. * Uses internal DateTime formatting, hence PHP's internal locale will be used (change using `setlocale()`).
  170. *
  171. * @param \DateTime|int|string $until maximum timestamp, defaults to "now"
  172. *
  173. * @example 'Tuesday'
  174. *
  175. * @see setlocale
  176. * @see https://www.php.net/manual/en/function.setlocale.php Set a different output language
  177. */
  178. public function dayOfWeek($until = 'now'): string;
  179. /**
  180. * Get a random month (numbered).
  181. *
  182. * @param \DateTime|int|string $until maximum timestamp, defaults to "now"
  183. *
  184. * @example '7'
  185. */
  186. public function month($until = 'now'): string;
  187. /**
  188. * Get a random month.
  189. *
  190. * @param \DateTime|int|string $until maximum timestamp, defaults to "now"
  191. *
  192. * @see setlocale
  193. * @see https://www.php.net/manual/en/function.setlocale.php Set a different output language
  194. *
  195. * @example 'September'
  196. */
  197. public function monthName($until = 'now'): string;
  198. /**
  199. * Get a random year between 1970 and `$until`.
  200. *
  201. * @param \DateTime|int|string $until maximum timestamp, defaults to "now"
  202. *
  203. * @example '1987'
  204. */
  205. public function year($until = 'now'): string;
  206. /**
  207. * Get a random century, formatted as Roman numerals.
  208. *
  209. * @example 'XVII'
  210. */
  211. public function century(): string;
  212. /**
  213. * Get a random timezone, uses `\DateTimeZone::listIdentifiers()` internally.
  214. *
  215. * @param string|null $countryCode two-letter ISO 3166-1 compatible country code
  216. *
  217. * @example 'Europe/Rome'
  218. */
  219. public function timezone(string $countryCode = null): string;
  220. }