Date and Time

Nette\Utils\DateTime is a class extends native DateTime with useful functions for working with date and time.

All examples assume the following class alias is defined:

use Nette\Utils\DateTime;

Let´s take a look how it´s simple.

$netteRelease = DateTime::from('2006-01-23 10:54:00'); // create from string
$netteRelease->modify('+10 years, 1 month'); // add 10 years and 1 month
echo $netteRelease; // 2016-02-23 10:54:00
$birthday = DateTime::createFromFormat('d.m.Y', '26.02.1994'); // create from custom format
echo $birthday->getTimestamp(); // 762265863

modify() is method from parent class DateTime.

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

Create Nette\Utils\DateTime instance from string, integer or other DateTime object and sets default timezone.

DateTime::from(1138013640); // create from timestamp
DateTime::from(42); // create from current time and add 42 seconds
DateTime::from('1994-02-26 04:15:32'); // create from date and time
DateTime::from('1994-02-26'); // create from date then fill zeroes

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

Extends from method for set custom format and timezone.

DateTime::createFromFormat('d.m.Y', '26.02.1994'); // create from custom format and default timezone
DateTime::createFromFormat('d.m.Y', '26.02.1994', 'Europe/London'); // create with custom timezone
DateTime::createFromFormat('j. n. Y H:i', '8. 9. 2042 16:36'); // other example

setTimestamp(int|string $timestamp): static

$created = new DateTime;

getTimestamp(): int|string

$publicDate = DateTime::from('2017-02-03');
$publicDate->getTimestamp(); //1486076400

modifyClone(string $modify=''): static

Create independent copy of current instance.

$created = DateTime::from('2017-02-03');
$public = $created->modifyClone('+1 day');
echo $created; // 2017-02-03
echo $public; // 2017-02-04

implements JsonSerializable

Get string in standard ISO 8601 (used by JavaScript).

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