Nette Documentation Preview

syntax
Date and Time
*************

.[perex]
[api:Nette\Utils\DateTime] is a class that extends the native [php:DateTime] with additional useful features.

Installation:

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

All examples assume the following class alias is defined:

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


static from(string|int|\DateTimeInterface $time): static .[method]
------------------------------------------------------------------
Creates a `DateTime` object from a string, UNIX timestamp, or another [php:DateTimeInterface] object. Throws an `\Exception` if the date and time are not valid.

```php
DateTime::from(1138013640); // creates a DateTime from the UNIX timestamp with the default timezone
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'); // creates DateTime from 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): static .[method]
------------------------------------------------------------------------------------------------------------------
Creates a `DateTime` object or throws an `Nette\InvalidArgumentException` if the date and time are not valid.
```php
DateTime::fromParts(1994, 2, 26, 4, 15, 32);
```


static createFromFormat(string $format, string $time, string|\DateTimeZone|null $timezone=null): static|false .[method]
-----------------------------------------------------------------------------------------------------------------------
Extends [php:DateTime::createFromFormat] with the ability to specify a timezone as a string.
```php
DateTime::createFromFormat('d.m.Y', '26.02.1994', 'Europe/London'); // create with custom timezone
```


modifyClone(string $modify=''): static .[method]
------------------------------------------------
Creates a copy with a modified time.
```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]
------------------------------
Returns the date and time in the format `Y-m-d H:i:s`.
```php
echo $dateTime; // '2017-02-03 04:15:32'
```


implements JsonSerializable .[method]
-------------------------------------
Returns the date and time in ISO 8601 format (e.g., `2017-02-03T04:15:32.123+01:00`), which is commonly used in JavaScript.
```php
$date = DateTime::from('2017-02-03');
echo json_encode($date);
```

Date and Time

Nette\Utils\DateTime is a class that extends the native DateTime with additional useful features.

Installation:

composer require nette/utils

All examples assume the following class alias is defined:

use Nette\Utils\DateTime;

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

Creates a DateTime object from a string, UNIX timestamp, or another DateTimeInterface object. Throws an \Exception if the date and time are not valid.

DateTime::from(1138013640); // creates a DateTime from the UNIX timestamp with the default timezone
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'); // creates DateTime from 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)static

Creates a DateTime object or throws an Nette\InvalidArgumentException if the date and time are not valid.

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

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

Extends DateTime::createFromFormat with the ability to specify a timezone as a string.

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

modifyClone(string $modify='')static

Creates a copy with a modified time.

$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

Returns the date and time in the format Y-m-d H:i:s.

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

implements JsonSerializable

Returns the date and time in ISO 8601 format (e.g., 2017-02-03T04:15:32.123+01:00), which is commonly used in JavaScript.

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