PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Faker/Provider/DateTime.php

https://github.com/gobb/Faker
PHP | 188 lines | 85 code | 22 blank | 81 comment | 0 complexity | 6ae5905b837889242d94d3c2e67e239b 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','XX1');
  6. /**
  7. * Get a timestamp between January 1, 1970 and now
  8. *
  9. * @example 1061306726
  10. */
  11. public static function unixTime()
  12. {
  13. return mt_rand(0, time());
  14. }
  15. /**
  16. * Get a datetime object for a date between January 1, 1970 and now
  17. *
  18. * @example DateTime('2005-08-16 20:39:21')
  19. * @return \DateTime
  20. */
  21. public static function dateTime()
  22. {
  23. return new \DateTime('@' . static::unixTime());
  24. }
  25. /**
  26. * Get a datetime object for a date between January 1, 001 and now
  27. *
  28. * @example DateTime('1265-03-22 21:15:52')
  29. * @return \DateTime
  30. */
  31. public static function dateTimeAD()
  32. {
  33. return new \DateTime('@' . mt_rand(-62135597361, time()));
  34. }
  35. /**
  36. * @example '2003-10-21T16:05:52+0000'
  37. */
  38. public static function iso8601()
  39. {
  40. return static::date(\DateTime::ISO8601);
  41. }
  42. /**
  43. * Get a date string between January 1, 1970 and now
  44. *
  45. * @param string $format
  46. * @example '2008-11-27'
  47. */
  48. public static function date($format = 'Y-m-d')
  49. {
  50. return static::dateTime()->format($format);
  51. }
  52. /**
  53. * Get a time string (24h format by default)
  54. *
  55. * @param string $format
  56. * @example '15:02:34'
  57. */
  58. public static function time($format = 'H:i:s')
  59. {
  60. return static::dateTime()->format($format);
  61. }
  62. /**
  63. * Get a DateTime object based on a random date between two given dates.
  64. * Accepts date strings that can be recognized by strtotime().
  65. *
  66. * @param string $startDate Defaults to 30 years ago
  67. * @param string $endDate Defaults to "now"
  68. * @example DateTime('1999-02-02 11:42:52')
  69. * @return \DateTime
  70. */
  71. public static function dateTimeBetween($startDate = "-30 years", $endDate = "now")
  72. {
  73. $startTimestamp = $startDate instanceof \DateTime ? $startDate->getTimestamp() : strtotime($startDate);
  74. $endTimestamp = $endDate instanceof \DateTime ? $endDate->getTimestamp() : strtotime($endDate);
  75. $timestamp = mt_rand($startTimestamp, $endTimestamp);
  76. return new \DateTime('@' . $timestamp);
  77. }
  78. /**
  79. * @example DateTime('1964-04-04 11:02:02')
  80. * @return \DateTime
  81. */
  82. public static function dateTimeThisCentury()
  83. {
  84. return static::dateTimeBetween("-100 year");
  85. }
  86. /**
  87. * @example DateTime('2010-03-10 05:18:58')
  88. * @return \DateTime
  89. */
  90. public static function dateTimeThisDecade()
  91. {
  92. return static::dateTimeBetween("-10 year");
  93. }
  94. /**
  95. * @example DateTime('2011-09-19 09:24:37')
  96. * @return \DateTime
  97. */
  98. public static function dateTimeThisYear()
  99. {
  100. return static::dateTimeBetween("-1 year");
  101. }
  102. /**
  103. * @example DateTime('2011-10-05 12:51:46')
  104. * @return \DateTime
  105. */
  106. public static function dateTimeThisMonth()
  107. {
  108. return static::dateTimeBetween("-1 month");
  109. }
  110. /**
  111. * @example 'am'
  112. */
  113. public static function amPm()
  114. {
  115. return static::dateTime()->format('a');
  116. }
  117. /**
  118. * @example '22'
  119. */
  120. public static function dayOfMonth()
  121. {
  122. return static::dateTime()->format('d');
  123. }
  124. /**
  125. * @example 'Tuesday'
  126. */
  127. public static function dayOfWeek()
  128. {
  129. return static::dateTime()->format('l');
  130. }
  131. /**
  132. * @example '7'
  133. */
  134. public static function month()
  135. {
  136. return static::dateTime()->format('m');
  137. }
  138. /**
  139. * @example 'September'
  140. */
  141. public static function monthName()
  142. {
  143. return static::dateTime()->format('F');
  144. }
  145. /**
  146. * @example 1673
  147. */
  148. public static function year()
  149. {
  150. return static::dateTime()->format('Y');
  151. }
  152. /**
  153. * @example 'XVII'
  154. */
  155. public static function century()
  156. {
  157. return static::randomElement(static::$century);
  158. }
  159. /**
  160. * @example 'Europe/Paris'
  161. */
  162. public static function timezone()
  163. {
  164. return static::randomElement(\DateTimeZone::listIdentifiers());
  165. }
  166. }