DateTimeImmutable::__construct

date_create_immutable

(PHP 5 >= 5.5.0)

DateTimeImmutable::__construct -- date_create_immutableВозвращает новый объект DateTimeImmutable

Описание

Объектно-ориентированный стиль

public DateTimeImmutable::__construct ([ string $time = "now" [, DateTimeZone $timezone = NULL ]] )

Процедурный стиль

DateTimeImmutable date_create_immutable ([ string $time = "now" [, DateTimeZone $timezone = NULL ]] )

Данный конструктор похож на DateTime::__construct(), но работает с DateTimeImmutable.

Коментарии

Автор:
"If $timezone is omitted or null, the current timezone will be used." - note, that timezone IS NOT equal offset, if its important for your application.

If default timezone = Europe/Moscow, then:
echo (new \DateTimeImmutable('2014-10'))->format(DATE_ATOM); // gives "2014-10-01T00:00:00+04:00"
echo (new \DateTimeImmutable('2014-11'))->format(DATE_ATOM); // gives "2014-11-01T00:00:00+03:00"
because of law changes (abolition of "summer time").
2023-04-05 13:58:44
http://php5.kiev.ua/manual/ru/datetimeimmutable.construct.html
Working on a (REST) interface between JavaScript and a database needs to take care of the problem of the time zone info. In JavaScript JSON.stringify() will convert all dates to UTC. Makes sense. If we receive and decode this on in the PHP realm we should explicitly say so and altought null indicates the configured locale timezone although I do believe that more often than not no timezone is configured in PHP. So better be save than sorry and prevent you to miss your flight:

<?php
$jsonString 
'{ "date": "2025-05-04T11:58:37.848Z" }';
$dateString json_decode($jsonStringtrue)['date']; // will contain the date in UTC (Zulu) tz

$d = new \DateTimeImmutable($dateString, new \DateTimeZone('UTC')); // interpreted as such

$databaseZone = new \DateTimeZone("Europe/Zurich");
$d $d->setTimeZone$databaseZone ); // but our server is somewhere else

var_dump($d);

// Now we can store the date in our local database, which is blissfully unaware of timezones

?>
2025-05-04 15:42:19
http://php5.kiev.ua/manual/ru/datetimeimmutable.construct.html

    Поддержать сайт на родительском проекте КГБ