International PHP Conference Berlin 2025

The IntlDateFormatter class

(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL intl >= 1.0.0)

Introduction

Date Formatter is a concrete class that enables locale-dependent formatting/parsing of dates using pattern strings and/or canned patterns.

This class represents the ICU date formatting functionality. It allows users to display dates in a localized format or to parse strings into PHP date values using pattern strings and/or canned patterns.

Class synopsis

class IntlDateFormatter {
/* Constants */
public const int FULL;
public const int LONG;
public const int MEDIUM;
public const int SHORT;
public const int NONE;
public const int RELATIVE_FULL;
public const int RELATIVE_LONG;
public const int RELATIVE_MEDIUM;
public const int RELATIVE_SHORT;
public const int GREGORIAN;
public const int TRADITIONAL;
/* Methods */
public __construct(
    ?string $locale,
    int $dateType = IntlDateFormatter::FULL,
    int $timeType = IntlDateFormatter::FULL,
    IntlTimeZone|DateTimeZone|string|null $timezone = null,
    IntlCalendar|int|null $calendar = null,
    ?string $pattern = null
)
public static 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
public static formatObject(IntlCalendar|DateTimeInterface $datetime, array|int|string|null $format = null, ?string $locale = null): string|false
public getErrorCode(): int
public isLenient(): bool
public localtime(string $string, int &$offset = null): array|false
public parse(string $string, int &$offset = null): int|float|false
public setCalendar(IntlCalendar|int|null $calendar): bool
public setLenient(bool $lenient): void
public setPattern(string $pattern): bool
}

Predefined Constants

These constants are used to specify different formats in the constructor for DateType and TimeType.

IntlDateFormatter::NONE int
Do not include this element
IntlDateFormatter::FULL int
Completely specified style (Tuesday, April 12, 1952 AD or 3:30:42pm PST)
IntlDateFormatter::LONG int
Long style (January 12, 1952 or 3:30:32pm)
IntlDateFormatter::MEDIUM int
Medium style (Jan 12, 1952)
IntlDateFormatter::SHORT int
Most abbreviated style, only essential data (12/13/52 or 3:30pm)
IntlDateFormatter::RELATIVE_FULL int
The same as IntlDateFormatter::FULL, but yesterday, today, and tomorrow show as yesterday, today, and tomorrow, respectively. Available as of PHP 8.0.0, for dateType only.
IntlDateFormatter::RELATIVE_LONG int
The same as IntlDateFormatter::LONG, but yesterday, today, and tomorrow show as yesterday, today, and tomorrow, respectively. Available as of PHP 8.0.0, for dateType only.
IntlDateFormatter::RELATIVE_MEDIUM int
The same as IntlDateFormatter::MEDIUM, but yesterday, today, and tomorrow show as yesterday, today, and tomorrow, respectively. Available as of PHP 8.0.0, for dateType only.
IntlDateFormatter::RELATIVE_SHORT int
The same as IntlDateFormatter::SHORT, but yesterday, today, and tomorrow show as yesterday, today, and tomorrow, respectively. Available as of PHP 8.0.0, for dateType only.

The following int constants are used to specify the calendar. These calendars are all based directly on the Gregorian calendar. Non-Gregorian calendars need to be specified in locale. Examples might include locale="hi@calendar=BUDDHIST".

IntlDateFormatter::TRADITIONAL int
Non-Gregorian Calendar
IntlDateFormatter::GREGORIAN int
Gregorian Calendar

Changelog

Version Description
8.4.0 The class constants are now typed.

Table of Contents

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top