-
Notifications
You must be signed in to change notification settings - Fork 159
Date and Time
Dmitriy Zayceff edited this page May 7, 2015
·
7 revisions
There is a few classes to work with date, time and timezones:
-
php\time\Time
to work with datetime. -
php\time\TimeZone
to work with timezones. -
php\time\TimeFormat
to parse and format time.
-
DateTime
->Time
,TimeZone
-
time
->Time::seconds()
unix time in seconds (UTC),Time::now
,Time::today
-
???
->Time::millis()
unix time in milliseconds -
microtime(1)
->Time::millis() / 1000
-
strtotime
->TimeFormat::parse
-
date
->TimeFormat::format
use php\time\Time;
$time = Time::now();
echo $time->toString('d-m-Y');
// or you can use TimeFormat
$tf = new TimeFormat('d-m-Y');
echo $tf->format(Time::now());
use php\time\Time;
use php\time\TimeZone;
$moscowTZ = TimeZone::of('Europe/Moscow');
$time = Time::now(TimeZone::UTC());
$time = Time::now($moscowTZ);
// 01 Jan 2013, Moscow timezone
$time = Time::of(['year' => 2013, 'month' => 1, 'day' => 1], $moscowTZ);
What use instead of
DateTimeZone::listIdentifiers
andtimezone_identifiers_list
?
use php\time\TimeZone;
var_dump(TimeZone::getAvailableIDs());
Use the Time::getTime()
method.
use php\time\Time;
$stamp = Time::now()->getTime();
$now = Time::of(['year' => 2010, 'month' => 1, 'day' => 15]); // 2010-01-15
$time = $now->replace(['month' => 5, 'year' => 2020]); // 2020-05-15
// or add ...
$time = $now->add(['year' => 5]); // 2015-01-15
You also can use day
, sec
, min
, hour
, millis
.
JPHP Group 2015