Interoperability

composer require php-standard-library/interoperability

The Interoperability component provides a set of interfaces for converting between PSL types and their PHP standard library (stdlib) or intl extension equivalents.

These interfaces establish a uniform contract for bidirectional conversion, allowing PSL types to interoperate seamlessly with native PHP types.

Usage

use Psl\DateTime\DateTime;
use Psl\DateTime\Timezone;
use Psl\IO;

// Convert a PSL DateTime to a PHP DateTimeImmutable
$psl = DateTime::fromParts(Timezone::AmericaNewYork, 2024, 6, 15, 14, 30, 45);
$stdlib = $psl->toStdlib();
IO\write_line('PSL to stdlib: %s', $stdlib->format('Y-m-d H:i:s T'));

// Convert back from a PHP DateTimeImmutable to a PSL DateTime
$back = DateTime::fromStdlib($stdlib);
IO\write_line('Back to PSL: %s', $back->toRfc3339());

// Convert a PSL Timezone to an IntlTimeZone
$intl = Timezone::AmericaNewYork->toIntl();
$id = $intl->getID();
IO\write_line('IntlTimeZone ID: %s', false === $id ? '<unknown>' : $id);

// Convert back from an IntlTimeZone to a PSL Timezone
$tz = Timezone::fromIntl($intl);
IO\write_line('Back to PSL Timezone: %s', $tz->value);

Conversion Interfaces

The component defines four interfaces covering all conversion directions:

Implementations

The following PSL types implement these interfaces:

PSL Type ToStdlib FromStdlib ToIntl FromIntl
DateTime\DateTime DateTimeImmutable DateTimeImmutable IntlCalendar IntlCalendar
DateTime\Timestamp DateTimeImmutable DateTimeImmutable - -
DateTime\Duration DateInterval - - -
DateTime\Timezone DateTimeZone DateTimeZone IntlTimeZone IntlTimeZone

See src/Psl/Interoperability/ for the full API.