Skip to content

Commit 7055bdc

Browse files
committed
docs: add documentation on date/time formatting to README
1 parent e7f1e2d commit 7055bdc

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

README.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,103 @@ echo $formatphp->formatMessage([
8282
]);
8383
```
8484

85+
### Formatting Dates and Times
86+
87+
You may use the methods `formatDate()` and `formatTime()` to format dates and
88+
times.
89+
90+
```php
91+
use FormatPHP\Config;
92+
use FormatPHP\FormatPHP;
93+
use FormatPHP\Intl;
94+
95+
$config = new Config(new Intl\Locale('es'));
96+
$formatphp = new FormatPHP($config);
97+
98+
$date = new DateTimeImmutable();
99+
100+
echo $formatphp->formatDate($date); // e.g. "21/1/22"
101+
echo $formatphp->formatTime($date); // e.g. "16:58"
102+
```
103+
104+
#### Using Intl\DateTimeFormatOptions with formatDate() and formatTime()
105+
106+
Fine-tune date and time formatting with `Intl\DateTimeFormatOptions`.
107+
108+
```php
109+
echo $formatphp->formatDate($date, new Intl\DateTimeFormatOptions([
110+
'dateStyle' => 'medium',
111+
])); // e.g. "21 ene 2022"
112+
echo $formatphp->formatTime($date, new Intl\DateTimeFormatOptions([
113+
'timeStyle' => 'long',
114+
])); // e.g. "16:58:31 UTC"
115+
```
116+
117+
`DateTimeFormatOptions` accepts the following general options for formatting
118+
dates and times:
119+
120+
* `dateStyle`: General formatting of the date, according to the locale. Possible
121+
values include: `full`, `long`, `medium`, and `short`.
122+
* `timeStyle`: General formatting of the time, according to the locale. Possible
123+
values include: `full`, `long`, `medium`, and `short`.
124+
125+
> ℹ️ **Note:** `dateStyle` and `timeStyle` may be used together, but they cannot
126+
> be used with more granular formatting options (i.e., `era`, `year`, `month`,
127+
> `day`, `weekday`, `day`, `hour`, `minute`, or `second`).
128+
129+
Instead of `dateStyle` and `timeStyle`, you may use the following options for
130+
more granular formatting of dates and times:
131+
132+
* `era`: The locale representation of the era (e.g. "AD", "BC"). Possible values
133+
are: `long`, `short`, and `narrow`.
134+
* `year`: The locale representation of the year. Possible values are: `numeric`
135+
or `2-digit`.
136+
* `month`: The locale representation of the month. Possible values are: `numeric`,
137+
`2-digit`, `long`, `short`, or `narrow`.
138+
* `weekday`: The locale representation of the weekday name. Possible values are:
139+
`long`, `short`, and `narrow`.
140+
* `day`: The locale representation of the day. Possible values are: `numeric` or
141+
`2-digit`.
142+
* `hour`: The locale representation of the hour. Possible values are: `numeric`
143+
or `2-digit`.
144+
* `minute`: The locale representation of the minute. Possible values are:
145+
`numeric` or `2-digit`.
146+
* `second`: The locale representation of the seconds. Possible values are:
147+
`numeric` or `2-digit`.
148+
149+
Additional options include:
150+
151+
* `calendar`: The calendar system to use. Possible values include: `buddhist`,
152+
`chinese`, `coptic`, `dangi`, `ethioaa`, `ethiopia`, `ethiopic`, `gregory`,
153+
`hebrew`, `indian`, `islamic`, `islamic-civil`, `islamic-rgsa`, `islamic-tbla`,
154+
`islamic-umalqura`, `iso8601`, `japanese`, `persian`, or `roc`.
155+
* `dayPeriod`: The formatting style used for day periods like "in the morning",
156+
"am", "noon", "n" etc. Values include: `narrow`, `short`, or `long`.
157+
* `fractionalSecondDigits`: The number of digits used to represent fractions of
158+
a second (any additional digits are truncated). Values may be: `0`, `1`, `2`,
159+
or `3`.
160+
* `hour12`: If `true`, `hourCycle` will be `h12`, if `false`, `hourCycle` will
161+
be `h23`. This property overrides any value set by `hourCycle`.
162+
* `hourCycle`: The hour cycle to use. Values include: `h11`, `h12`, `h23`, and
163+
`h24`. If specified, this property overrides the `hc` property of the locale's
164+
language tag. The `hour12` property takes precedence over this value.
165+
* `numberingSystem`: The numeral system to use. Possible values include: `arab`,
166+
`arabext`, `armn`, `armnlow`, `bali`, `beng`, `brah`, `cakm`, `cham`, `deva`,
167+
`diak`, `ethi`, `finance`, `fullwide`, `geor`, `gong`, `gonm`, `grek`, `greklow`,
168+
`gujr`, `guru`, `hanidays`, `hanidec`, `hans`, `hansfin`, `hant`, `hantfin`,
169+
`hebr`, `hmnp`, `java`, `jpan`, `jpanfin`, `jpanyear`, `kali`, `khmr`, `knda`,
170+
`lana`, `lanatham`, `laoo`, `lepc`, `limb`, `mlym`, `mong`, `mtei`, `mymr`,
171+
`mymrshan`, `native`, `nkoo`, `olck`, `orya`, `osma`, `rohg`, `roman`, `romanlow`,
172+
`saur`, `shrd`, `sora`, `sund`, `takr`, `talu`, `taml`, `tamldec`, `telu`,
173+
`thai`, `tibt`, `tnsa`, `traditional`, `vaii`, or `wcho`.
174+
* `timeZoneName`: An indicator for how to format the localized representation of
175+
the time zone name. Values include: `long`, `short`, `shortOffset`, `longOffset`,
176+
`shortGeneric`, or `longGeneric`.
177+
* `timeZone`: The time zone to use. The default is the system's default time zone
178+
(see [date_default_timezone_set()](https://www.php.net/date_default_timezone_set)).
179+
You may use the zone names of the [IANA time zone database](https://www.iana.org/time-zones),
180+
such as "Asia/Shanghai", "Asia/Kolkata", "America/New_York".
181+
85182
### Rich Text Formatting (Use of Tags in Messages)
86183

87184
While the ICU message syntax does not prohibit the use of HTML tags in formatted

src/Intl/DateTimeFormatOptions.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,23 @@ class DateTimeFormatOptions implements JsonSerializable
9191
public ?string $timeStyle = null;
9292

9393
/**
94+
* The calendar system to use
95+
*
9496
* @var CalendarType | null
9597
*/
9698
public ?string $calendar = null;
9799

98100
/**
101+
* The formatting style used for day periods like "in the morning", "am",
102+
* "noon", "n" etc.
103+
*
99104
* @var PeriodType | null
100105
*/
101106
public ?string $dayPeriod = null;
102107

103108
/**
109+
* The numeral system to use
110+
*
104111
* @var NumeralType | null
105112
*/
106113
public ?string $numberingSystem = null;
@@ -117,59 +124,92 @@ class DateTimeFormatOptions implements JsonSerializable
117124
*/
118125
public ?string $timeZone = null;
119126

127+
/**
128+
* If true, hourCycle will be "h12," if false, hourCycle will be "h23"
129+
*
130+
* This property overrides any value set by hourCycle.
131+
*/
120132
public ?bool $hour12 = null;
121133

122134
/**
135+
* The hour cycle to use
136+
*
137+
* If this property is specified, it overrides the hc property of the
138+
* language tag, if set. The hour12 property takes precedence over
139+
* this value.
140+
*
123141
* @var HourType | null
124142
*/
125143
public ?string $hourCycle = null;
126144

127145
/**
146+
* The locale representation of the weekday name.
147+
*
128148
* @var PeriodType | null
129149
*/
130150
public ?string $weekday = null;
131151

132152
/**
153+
* The locale representation of the era (e.g. "AD", "BC")
154+
*
133155
* @var PeriodType | null
134156
*/
135157
public ?string $era = null;
136158

137159
/**
160+
* The locale representation of the year
161+
*
138162
* @var WidthType | null
139163
*/
140164
public ?string $year = null;
141165

142166
/**
167+
* The locale representation of the month
168+
*
143169
* @var WidthType | PeriodType | null
144170
*/
145171
public ?string $month = null;
146172

147173
/**
174+
* The locale representation of the day
175+
*
148176
* @var WidthType | null
149177
*/
150178
public ?string $day = null;
151179

152180
/**
181+
* The locale representation of the hour
182+
*
153183
* @var WidthType | null
154184
*/
155185
public ?string $hour = null;
156186

157187
/**
188+
* The locale representation of the minute
189+
*
158190
* @var WidthType | null
159191
*/
160192
public ?string $minute = null;
161193

162194
/**
195+
* The locale representation of the seconds
196+
*
163197
* @var WidthType | null
164198
*/
165199
public ?string $second = null;
166200

167201
/**
202+
* The number of digits used to represent fractions of a second (any
203+
* additional digits are truncated)
204+
*
168205
* @var FractionDigitsType | null
169206
*/
170207
public ?int $fractionalSecondDigits = null;
171208

172209
/**
210+
* An indicator for how to format the localized representation of the time
211+
* zone name
212+
*
173213
* @var TimeZoneNameType | null
174214
*/
175215
public ?string $timeZoneName = null;

0 commit comments

Comments
 (0)