Nette Documentation Preview

syntax
Data e ora
**********

.[perex]
[api:Nette\Utils\DateTime] è una classe che estende il nativo [php:DateTime].


Installazione:

```shell
composer require nette/utils
```

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

```php
use Nette\Utils\DateTime;
```


static from(string|int|\DateTimeInterface $time): DateTime .[method]
--------------------------------------------------------------------
Crea un oggetto DateTime da una stringa, un timestamp UNIX o un altro oggetto [php:DateTimeInterface]. Lancia un `Exception` se la data e l'ora non sono valide.

```php
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 .[method]
--------------------------------------------------------------------------------------------------------------------
Crea un oggetto DateTime o lancia un'eccezione `Nette\InvalidArgumentException` se la data e l'ora non sono valide.
```php
DateTime::fromParts(1994, 2, 26, 4, 15, 32);
```


static createFromFormat(string $format, string $time, string|\DateTimeZone $timezone=null): DateTime|false .[method]
--------------------------------------------------------------------------------------------------------------------
Estende [DateTime::createFromFormat() |https://www.php.net/manual/en/datetime.createfromformat.php] con la possibilità di specificare un fuso orario come stringa.
```php
DateTime::createFromFormat('d.m.Y', '26.02.1994', 'Europe/London'); // create with custom timezone
```


modifyClone(string $modify=''): static .[method]
------------------------------------------------
Crea una copia con un'ora modificata.
```php
$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 .[method]
------------------------------
Restituisce la data e l'ora nel formato `Y-m-d H:i:s`.
```php
echo $dateTime; // '2017-02-03 04:15:32'
```


Implementa JsonSerializable .[#toc-implements-jsonserializable]
---------------------------------------------------------------
Restituisce la data e l'ora nel formato ISO 8601, utilizzato ad esempio in JavaScript.
```php
$date = DateTime::from('2017-02-03');
echo json_encode($date);
```

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