You are here

public static function DateTimePlus::createFromTimestamp in Drupal 8

Same name and namespace in other branches
  1. 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()
DateTimePlusTest::testTimestamp in core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php
Test creating dates from timestamps, and manipulating timezones.

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;
}