Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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