shlogg · Early preview
Paul Redmond @paulredmond

PHP DateTime From Unix Timestamp Made Easy With CreateFromTimestamp()

PHP 8.4 introduces createFromTimestamp() method for DateTimeImmutable, supporting Unix timestamps with microseconds. Replaces createFromFormat() in PHP 8.3 and below. Carbon library already has this feature.

Creating a DateTime from a Unix timestamp will be more convenient in PHP 8.4 with the new createFromTimestamp() method. It will support both a typical Unix timestamp as well as timestamps containing microseconds:
$dt = DateTimeImmutable::createFromTimestamp(1718337072);$dt->format('Y-m-d'); // 2024-06-14 $dt = DateTimeImmutable::createFromTimestamp(1718337072.432);$dt->format('Y-m-d h:i:s.u'); // 2024-06-14 03:51:12.432000
With PHP 8.3 and below, you need to use the createFromFormat() method to convert a timestamp into a DateTime or DateTimeImmutable instance. As you can see below, it's not to...