Data e ora

Nette\Utils\DateTime è una classe che estende il nativo DateTime.

Installazione:

composer require nette/utils

Tutti gli esempi presuppongono che sia definito il seguente alias di classe:

use Nette\Utils\DateTime;

static from(string|int|\DateTimeInterface $time): DateTime

Crea un oggetto DateTime da una stringa, un timestamp UNIX o un altro oggetto DateTimeInterface. Lancia un Exception se la data e l'ora non sono valide.

DateTime::from(1138013640); // creates a DateTime from the UNIX timestamp with a default timezamp
DateTime::from(42); // creates a DateTime from the current time plus 42 seconds
DateTime::from('1994-02-26 04:15:32'); // creates a DateTime based on a string
DateTime::from('1994-02-26'); // create DateTime by date, time will be 00:00:00

static fromParts(int $year, int $month, int $day, int $hour=0, int $minute=0, float $second=0.0): DateTime

Crea un oggetto DateTime o lancia un'eccezione Nette\InvalidArgumentException se la data e l'ora non sono valide.

DateTime::fromParts(1994, 2, 26, 4, 15, 32);

static createFromFormat(string $format, string $time, string|\DateTimeZone $timezone=null): DateTime|false

Estende DateTime::createFromFormat() con la possibilità di specificare un fuso orario come stringa.

DateTime::createFromFormat('d.m.Y', '26.02.1994', 'Europe/London'); // create with custom timezone

modifyClone(string $modify='')static

Crea una copia con un'ora modificata.

$original = DateTime::from('2017-02-03');
$clone = $original->modifyClone('+1 day');
$original->format('Y-m-d'); // '2017-02-03'
$clone->format('Y-m-d');    // '2017-02-04'

__toString(): string

Restituisce la data e l'ora nel formato Y-m-d H:i:s.

echo $dateTime; // '2017-02-03 04:15:32'

Implementa JsonSerializable

Restituisce la data e l'ora nel formato ISO 8601, utilizzato ad esempio in JavaScript.

$date = DateTime::from('2017-02-03');
echo json_encode($date);
versione: 4.0