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