datefmt_create
funkcja
Tworzy obiekt IntlDateFormatter do lokalizowanego formatowania dat i godzin (np. 'pl_PL' → '15 czerwca 2024'). Wymaga rozszerzenia intl.
datefmt_create(?string $locale, int $dateType = IntlDateFormatter::FULL, int $timeType = IntlDateFormatter::FULL, IntlTimeZone|DateTimeZone|string|null $timezone = null, IntlCalendar|int|null $calendar = null, ?string $pattern = null): ?IntlDateFormatter
datefmt_create tworzy obiekt IntlDateFormatter, który formatuje daty i godziny zgodnie z konwencjami danej lokalizacji. W przeciwieństwie do date() i strftime(), używa reguł ICU — właściwa lokalizacja automatycznie dobiera nazwy miesięcy, kolejność elementów i separatory.
Stałe $dateType i $timeType:
| Stała | Przykład (pl_PL) |
|---|---|
| IntlDateFormatter::NONE | (puste) |
| IntlDateFormatter::SHORT | 15.06.2024 |
| IntlDateFormatter::MEDIUM | 15 cze 2024 |
| IntlDateFormatter::LONG | 15 czerwca 2024 |
| IntlDateFormatter::FULL | sobota, 15 czerwca 2024 |
Funkcja zwraca null przy błędzie. Wymaga rozszerzenia intl.
<?php
declare(strict_types=1);
// Tylko data, format długi, strefa czasowa Warszawa
$fmt = datefmt_create(
'pl_PL',
IntlDateFormatter::LONG,
IntlDateFormatter::NONE,
'Europe/Warsaw'
);
echo datefmt_format($fmt, mktime(0, 0, 0, 6, 15, 2024)); // 15 czerwca 2024
echo datefmt_format($fmt, mktime(0, 0, 0, 12, 1, 2024)); // 1 grudnia 2024
echo datefmt_format($fmt, mktime(0, 0, 0, 1, 15, 2025)); // 15 stycznia 2025
// Data i godzina dla angielskiej lokalizacji
$en = datefmt_create('en_US', IntlDateFormatter::LONG, IntlDateFormatter::SHORT, 'UTC');
echo datefmt_format($en, mktime(14, 30, 0, 6, 15, 2024)); // June 15, 2024 at 2:30 PM