Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Component/Datetime/DateTimePlus.php \Drupal\Component\Datetime\DateTimePlus::createFromTimestamp()
  2. 9 core/lib/Drupal/Component/Datetime/DateTimePlus.php \Drupal\Component\Datetime\DateTimePlus::createFromTimestamp()

Creates a date object from timestamp input.

The timezone of a timestamp is always UTC. The timezone for a timestamp indicates the timezone used by the format() method.

Parameters

int $timestamp: A UNIX timestamp.

mixed $timezone: (optional) \DateTimeZone object, time zone string or NULL. See __construct() for more details.

array $settings: (optional) A keyed array for settings, suitable for passing on to __construct().

Return value

static A new DateTimePlus object.

Throws

\InvalidArgumentException If the timestamp is not numeric.

1 call to DateTimePlus::createFromTimestamp()
EntitySerializationTest::testSerialize in core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php
Tests entity serialization for core's formats by a registered Serializer.

File

core/lib/Drupal/Component/Datetime/DateTimePlus.php, line 199

Class

DateTimePlus
Wraps DateTime().

Namespace

Drupal\Component\Datetime

Code

public static function createFromTimestamp($timestamp, $timezone = NULL, $settings = []) {
  if (!is_numeric($timestamp)) {
    throw new \InvalidArgumentException('The timestamp must be numeric.');
  }
  $datetime = new static('', $timezone, $settings);
  $datetime
    ->setTimestamp($timestamp);
  return $datetime;
}