1. Go to this page and download the library: Download tiny-blocks/time library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tiny-blocks / time example snippets
declare(strict_types=1);
use TinyBlocks\Time\Instant;
$instant = Instant::now();
$instant->toIso8601(); # 2026-02-17T10:30:00+00:00
$instant->toUnixSeconds(); # 1771324200
$instant->toDateTimeImmutable(); # DateTimeImmutable (UTC, with microseconds)
declare(strict_types=1);
use TinyBlocks\Time\Instant;
use TinyBlocks\Time\Timezone;
$newYork = Timezone::from(identifier: 'America/New_York');
$instant = Instant::fromString(value: '2026-02-08T07:30:00+00:00');
# The local time 2026-02-08 02:30 plus one month targets the non-existent local 2026-03-08 02:30,
# so PHP resolves it forward to 03:30 -04:00.
$instant->plusMonths(months: 1, zone: $newYork)->toIso8601(); # 2026-03-08T07:30:00+00:00
declare(strict_types=1);
use TinyBlocks\Time\SystemMonotonicClock;
$clock = new SystemMonotonicClock();
$clock->nanoseconds(); # 12345678901234 (an opaque counter, not a calendar value)
declare(strict_types=1);
use TinyBlocks\Time\SystemMonotonicClock;
$clock = new SystemMonotonicClock();
$start = $clock->nanoseconds();
# Perform the operation whose latency is being measured.
usleep(1000);
$elapsedNanos = $clock->nanoseconds() - $start;
declare(strict_types=1);
use TinyBlocks\Time\Stopwatch;
use TinyBlocks\Time\SystemMonotonicClock;
$stopwatch = Stopwatch::start(clock: new SystemMonotonicClock());
declare(strict_types=1);
use TinyBlocks\Time\Stopwatch;
use TinyBlocks\Time\SystemMonotonicClock;
$stopwatch = Stopwatch::start(clock: new SystemMonotonicClock());
# Perform the operation whose latency is being measured.
usleep(1500);
$stopwatch->elapsed()->toMilliseconds(); # 1.5
declare(strict_types=1);
use TinyBlocks\Time\Stopwatch;
use TinyBlocks\Time\SystemMonotonicClock;
$stopwatch = Stopwatch::start(clock: new SystemMonotonicClock());
usleep(1000);
$firstReading = $stopwatch->elapsed();
usleep(1000);
$secondReading = $stopwatch->elapsed();
$firstReading->toMilliseconds(); # approximately 1.0
$secondReading->toMilliseconds(); # approximately 2.0
declare(strict_types=1);
use TinyBlocks\Time\Instant;
use TinyBlocks\Time\Period;
$period = Period::from(
from: Instant::fromString(value: '2026-02-17T10:00:00+00:00'),
to: Instant::fromString(value: '2026-02-17T11:00:00+00:00')
);
$period->from->toIso8601(); # 2026-02-17T10:00:00+00:00
$period->to->toIso8601(); # 2026-02-17T11:00:00+00:00
Period::from(from: $later, to: $earlier); # throws InvalidPeriod
declare(strict_types=1);
use TinyBlocks\Time\Duration;
use TinyBlocks\Time\Instant;
use TinyBlocks\Time\Period;
$period = Period::startingAt(
from: Instant::fromString(value: '2026-02-17T10:00:00+00:00'),
duration: Duration::fromMinutes(minutes: 90)
);
$period->from->toIso8601(); # 2026-02-17T10:00:00+00:00
$period->to->toIso8601(); # 2026-02-17T11:30:00+00:00
declare(strict_types=1);
use TinyBlocks\Time\LocalDate;
$date = LocalDate::fromString(value: '2026-05-23');
$date->toIso8601(); # 2026-05-23
declare(strict_types=1);
use TinyBlocks\Time\LocalDate;
use TinyBlocks\Time\Timezone;
$today = LocalDate::today(zone: Timezone::from(identifier: 'America/Sao_Paulo'));
$today->toIso8601(); # 2026-05-23
declare(strict_types=1);
use TinyBlocks\Time\Instant;
use TinyBlocks\Time\Timezone;
$instant = Instant::fromString(value: '2026-05-23T12:00:00+00:00');
$date = $instant->toLocalDate(zone: Timezone::utc());
$date->toIso8601(); # 2026-05-23
declare(strict_types=1);
use TinyBlocks\Time\LocalDate;
use TinyBlocks\Time\TimeOfDay;
use TinyBlocks\Time\Timezone;
$date = LocalDate::of(year: 2026, month: 6, day: 15);
$instant = $date->atTime(
time: TimeOfDay::from(hour: 0, minute: 0),
zone: Timezone::from(identifier: 'America/Sao_Paulo')
);
$instant->toIso8601(); # 2026-06-15T03:00:00+00:00
$springForward = LocalDate::of(year: 2026, month: 3, day: 8);
$newYork = Timezone::from(identifier: 'America/New_York');
# 02:30 does not exist on this day, so it lands where 03:30 lands.
$springForward->atTime(time: TimeOfDay::from(hour: 2, minute: 30), zone: $newYork)->toIso8601();
# 2026-03-08T07:30:00+00:00
declare(strict_types=1);
use TinyBlocks\Time\Instant;
$morning = Instant::fromString(value: '2026-02-17T08:27:21+00:00');
$sameMoment = Instant::fromString(value: '2026-02-17T05:27:21-03:00');
$morning->equals(other: $sameMoment); # true, both normalize to the same UTC moment
$morning->hashCode() === $sameMoment->hashCode(); # true