Skip to content

Commit 4eebfc6

Browse files
author
Bram Ceulemans
committed
Added documentation for date and time provider
1 parent 30ecd36 commit 4eebfc6

File tree

3 files changed

+308
-15
lines changed

3 files changed

+308
-15
lines changed

docs/formatters.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ realText($maxNbChars = 200, $indexSize = 2) // "And yet I wish you could manage
6969

7070
### `Faker\Provider\DateTime`
7171

72-
```php
72+
```
7373
unixTime($max = 'now') // 58781813
7474
dateTime($max = 'now', $timezone = null) // DateTime('2008-04-25 08:37:17', 'UTC')
7575
dateTimeAD($max = 'now', $timezone = null) // DateTime('1800-04-29 20:38:49', 'Europe/Paris')
@@ -92,9 +92,6 @@ century // 'VI'
9292
timezone // 'Europe/Paris'
9393
```
9494

95-
Methods accepting a `$timezone` argument default to `date_default_timezone_get()`. You can pass a custom timezone string
96-
to each method, or define a custom timezone for all time methods at once using `$faker::setDefaultTimezone($timezone)`.
97-
9895
### `Faker\Provider\Internet`
9996

10097
```php

docs/formatters/date-and-time.md

Lines changed: 296 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,296 @@
1+
# Date and Time
2+
3+
Methods accepting a `$timezone` argument default to `date_default_timezone_get()`. You can pass a custom timezone string
4+
to each method, or define a custom timezone for all time methods at once using `$faker::setDefaultTimezone($timezone)`.
5+
6+
## `unixTime`
7+
8+
Generate an [unix time](https://en.wikipedia.org/wiki/Unix_time) between zero, and the given value. By default, `now` is
9+
used as input.
10+
11+
Optionally, a parameter can be supplied containing a `DateTime`, `int` or `string`.
12+
13+
```php
14+
echo $faker->unixTime();
15+
16+
// 1605544623, 1025544612
17+
18+
echo $faker->unixTime(new DateTime('+3 weeks'));
19+
20+
// unix timestamp between 0 and the date 3 weeks from now.
21+
```
22+
23+
## `dateTime`
24+
25+
Generate a `DateTime` between January 1, 1970, and the given max. By default, `now` is used as max.
26+
27+
Optionally, a parameter can be supplied containing a `DateTime`, `int` or `string`.
28+
29+
An optional second parameter can be supplied, with the timezone.
30+
31+
```php
32+
echo $faker->dateTime();
33+
34+
// DateTime: August 12, 1991
35+
```
36+
37+
## `dateTimeAD`
38+
39+
Generate a `DateTime` between January 1, 0001, and the given max. By default, `now` is used as max.
40+
41+
An optional second parameter can be supplied, with the timezone.
42+
43+
```php
44+
echo $faker->dateTimeAD();
45+
46+
// DateTime: September 19, 718
47+
```
48+
49+
## `iso8601`
50+
51+
Generate an ISO8601 formatted `string` between January 1, 0001, and the given max. By default, `now` is used as max.
52+
53+
```php
54+
echo $faker->iso8601();
55+
```
56+
57+
## `date`
58+
59+
Generate a date `string`, with a given format and max value. By default, `'Y-m-d'` and `'now'` are used for the format
60+
and maximum value respectively.
61+
62+
```php
63+
echo $faker->date();
64+
65+
// '1999-06-09'
66+
67+
echo $faker->date('Y_m_d');
68+
69+
// '2011_02_19'
70+
```
71+
72+
## `time`
73+
74+
Generate a time `string`, with a given format and max value. By default, `H:i:s'` and `now` are used for the format and
75+
maximum value respectively.
76+
77+
```php
78+
echo $faker->time();
79+
80+
// '12:02:50'
81+
82+
echo $faker->time('H_i_s');
83+
84+
// '20_49_12'
85+
```
86+
87+
## `dateTimeBetween`
88+
89+
Generate a `DateTime` between two dates. By default, `-30 years` and `now` are used.
90+
91+
An optional third parameter can be supplied, with the timezone.
92+
93+
```php
94+
echo $faker->dateTimeBetween();
95+
96+
// a date between -30 years ago, and now
97+
98+
echo $faker->dateTimeBetween('-1 week', '+1 week');
99+
100+
// a date between -1 week ago, and 1 week from now
101+
```
102+
103+
## `dateTimeInInterval`
104+
105+
Generate a `DateTime` between a date and an interval from that date. By default, the date is set to `-30 years`, and the
106+
interval is set to `+5 days`.
107+
108+
An optional third parameter can be supplied, with the timezone.
109+
110+
```php
111+
echo $faker->dateTimeInInterval();
112+
113+
// a date between -30 years ago, and -30 years + 5 days
114+
115+
echo $faker->dateTimeInInterval('-1 week', '+3 days');
116+
117+
// a date between -1 week ago, and -1 week + 3 days
118+
```
119+
120+
## `dateTimeThisCentury`
121+
122+
Generate a `DateTime` that is within the current century. An optional `$max` value can be supplied, by default this is
123+
set to `now`.
124+
125+
An optional second parameter can be supplied, with the timezone.
126+
127+
```php
128+
echo $faker->dateTimeThisCentury();
129+
130+
// a date somewhere in this century
131+
132+
echo $faker->dateTimeThisCentury('+8 years');
133+
134+
// a date somewhere in this century, with an upper bound of +8 years
135+
```
136+
137+
## `dateTimeThisDecade`
138+
139+
Generate a `DateTime` that is within the current decade. An optional `$max` value can be supplied, by default this is
140+
set to `now`.
141+
142+
An optional second parameter can be supplied, with the timezone.
143+
144+
```php
145+
echo $faker->dateTimeThisDecade();
146+
147+
// a date somewhere in this decade
148+
149+
echo $faker->dateTimeThisDecade('+2 years');
150+
151+
// a date somewhere in this decade, with an upper bound of +2 years
152+
```
153+
154+
## `dateTimeThisYear`
155+
156+
Generate a `DateTime` that is within the current year. An optional `$max` value can be supplied, by default this is set
157+
to `now`.
158+
159+
An optional second parameter can be supplied, with the timezone.
160+
161+
```php
162+
echo $faker->dateTimeThisYear();
163+
164+
// a date somewhere in this year
165+
166+
echo $faker->dateTimeThisYear('+2 months');
167+
168+
// a date somewhere in this year, with an upper bound of +2 months
169+
```
170+
171+
## `dateTimeThisMonth`
172+
173+
Generate a `DateTime` that is within the current month. An optional `$max` value can be supplied, by default this is set
174+
to `now`.
175+
176+
An optional second parameter can be supplied, with the timezone.
177+
178+
```php
179+
echo $faker->dateTimeThisMonth();
180+
181+
// a date somewhere in this months
182+
183+
echo $faker->dateTimeThisMonth('+12 days');
184+
185+
// a date somewhere in this year, with an upper bound of +12 days
186+
```
187+
188+
## `amPm`
189+
190+
Generate a random `DateTime`, with a given upper bound, and return the am/pm `string` value. By default, the upper bound
191+
is set to `now`.
192+
193+
```php
194+
echo $faker->amPm();
195+
196+
// 'am'
197+
198+
echo $faker->amPm('+2 weeks');
199+
200+
// 'pm'
201+
```
202+
203+
## `dayOfMonth`
204+
205+
Generate a random `DateTime`, with a given upper bound, and return the day of month `string` value. By default, the
206+
upper bound is set to `now`.
207+
208+
```php
209+
echo $faker->dayOfMonth();
210+
211+
// '24'
212+
213+
echo $faker->dayOfMonth('+2 weeks');
214+
215+
// '05'
216+
```
217+
218+
## `dayOfWeek`
219+
220+
Generate a random `DateTime`, with a given upper bound, and return the day of week `string` value. By default, the
221+
upper bound is set to `now`.
222+
223+
```php
224+
echo $faker->dayOfWeek();
225+
226+
// 'Tuesday'
227+
228+
echo $faker->dayOfWeek('+2 weeks');
229+
230+
// 'Friday'
231+
```
232+
233+
## `month`
234+
235+
Generate a random `DateTime`, with a given upper bound, and return the month's number `string` value. By default, the
236+
upper bound is set to `now`.
237+
238+
```php
239+
echo $faker->month();
240+
241+
// '9'
242+
243+
echo $faker->month('+10 weeks');
244+
245+
// '7'
246+
```
247+
248+
## `monthName`
249+
250+
Generate a random `DateTime`, with a given upper bound, and return the month's name `string` value. By default, the
251+
upper bound is set to `now`.
252+
253+
```php
254+
echo $faker->monthName();
255+
256+
// 'September'
257+
258+
echo $faker->monthName('+10 weeks');
259+
260+
// 'April'
261+
```
262+
263+
## `year`
264+
265+
Generate a random `DateTime`, with a given upper bound, and return the year's `string` value. By default, the
266+
upper bound is set to `now`.
267+
268+
```php
269+
echo $faker->year();
270+
271+
// '1998'
272+
273+
echo $faker->year('+10 years');
274+
275+
// '2022'
276+
```
277+
278+
## `century`
279+
280+
Generate a random century name.
281+
282+
```php
283+
echo $faker->century();
284+
285+
// 'IX', 'XVII', 'II'
286+
```
287+
288+
## `timezone`
289+
290+
Generate a random timezone name.
291+
292+
```php
293+
echo $faker->century();
294+
295+
// 'Europe/Amsterdam', 'America/Montreal'
296+
```

mkdocs.yml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ theme:
1111
text: 'Nunito'
1212
code: 'Fira Code'
1313
palette:
14-
scheme: default
14+
scheme: preference
1515
primary: blue
1616
accent: blue
1717
icon:
@@ -81,6 +81,16 @@ extra:
8181
plugins:
8282
- search: { }
8383

84+
nav:
85+
- index.md
86+
- formatters.md
87+
- 'Formatters':
88+
- formatters/numbers-and-strings.md
89+
- formatters/text-and-paragraphs.md
90+
- formatters/date-and-time.md
91+
- 'Locales': *locales
92+
- third-party.md
93+
8494
markdown_extensions:
8595
- admonition: { }
8696
- toc:
@@ -94,13 +104,3 @@ markdown_extensions:
94104
startinline: true
95105
- pymdownx.superfences: { }
96106
- pymdownx.keys: { }
97-
98-
99-
nav:
100-
- index.md
101-
- formatters.md
102-
- 'Formatters':
103-
- formatters/numbers-and-strings.md
104-
- formatters/text-and-paragraphs.md
105-
- 'Locales': *locales
106-
- third-party.md

0 commit comments

Comments
 (0)