PHP code example of bakame / tokei

1. Go to this page and download the library: Download bakame/tokei 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/ */

    

bakame / tokei example snippets


Time::at(int $hour = 0, int $minute = 0, int $second = 0, int $microsecond = 0): Time;
Time::parse(string $value,  string $separator = ':'): ?Time
Time::fromUnitOfDay(int $value, Unit $unit): Time

use Bakame\Tokei\Time;

$timeB = Time::parse("10:30:15.123456");
$timeA = Time::at(hour: 10, minute: 30, second: 15);
$timeC = Time::fromUnitOfDay(123_456_789, Unit::Microsecond);
$timeC = Time::fromUnitOfDay(123_456, Unit::Millisecond);
$timeC = Time::fromUnitOfDay(123, Unit::Second);
$timeC = Time::fromUnitOfDay(456, Unit::Minute);
 
Time::midnight(); // 00:00:00
Time::noon();     // 12:00:00
Time::endOfDay(); // 23:59:59.999999

$time = Time::parse("10:30:15.123456");
$time->hour;         // returns 10
$time->minute;       // returns 30
$time->second;       // returns 15
$time->microsecond;  // returns 123456

Time::toUnitOfDay(Unit $unit): float; // returns the time value according to the provided
Time::toString(): string
Time::toLocaleString(string $locale, ?DateTimeZone $timezone = null): string

$time = Time::parse("10:30:15.123456");

$time->toString();              // 10:30:15.123456 (default)
$time->toMicroOfDay();          // 37815123456
$time->toLocaleString('en-US'); // "10:30:15 AM"

Time::add(Duration $duration): Time
Time::with(?int $hour = null, ?int $minute = null, ?int $second = null, ?int $microsecond = null): Time
Time::roundTo(Unit $precision, RoundingMode $roundingMode): Time
Time::clamp(Time $min, Time $max): Time

// adding 2 hours
$time = Time::noon()->add(Duration::of(hours: 2, minutes: 15));
$time->toString(); // returns "14:15:00"

// adding 12 hours
$time = Time::noon()->add(Duration::of(hours: 12, minutes: 15));
$time->toString(); // returns "00:15:00"

// setting the hour to
$time = Time::noon()->with(hour: 2);
$time->toString(); // returns "02:15:00"

Time::noon()->with(hour: 25); 
//throws a Bakame\Tokei\InvalidTime exception

$t = Time::fromUnitOfDay(3_150_000_000, Unit::Microsecond);
$t->toString();                            // returns "00:52:30"
$t->roundTo(Unit::Minutes, RoundingMode::Truncate)->toString(); // returns "00:52:00"
$t->roundTo(Unit::Minutes, RoundingMode::Round)->toString();    // returns "00:53:00"

Time::compareTo(Time $other): int;

$time = Time::at(hour: 10);
$other = Time::noon();

$time->isBefore($other);         // returns true
$time->isAfter($other);          // returns false
$time->isBeforeOrEqual($other);  // returns true
$time->isAfterOrEqual($other);   // returns false
$time->equals($other);           // returns false

Time::diff(Time $other): Duration;
Time::distance(Time $other): Duration;

$a = Time::at(hour: 23); // 23:00
$b = Time::at(hour: 1);  // 01:00

$a->diff($b)->toNotation(DurationNotation::Iso8601);     // returns "-PT22H"
$a->distance($b)->toNotation(DurationNotation::Iso8601); // returns "PT2H"

Time::fromDate(DateTimeInterface $datetime): Time
Time::applyTo(DateTimeInterface $datetime): DateTimeImmutable;

use Bakame\Tokei\Time;
use Carbon\Carbon;
use Carbon\CarbonImmutable;

$time = Time::fromDate(new DateTime('2025-12-27 23:00', new DateTimeZone('Africa/Nairobi'))); // 23:00

$newDate = $time->applyTo(CarbonImmutable::parse('2025-02-23'));
$newDate->toDateTimeString(); // returns '2025-02-23 23:00'
$newDate::class; // returns Carbon\CarbonImmutable

$altDate = $time->applyTo(Carbon::parse('2025-02-23'));
$altDate->toDateTimeString(); // returns '2025-02-23 23:00'
$altDate::class; // returns DateTimeImmutable

use Bakame\Tokei\Duration;

$durationA = Duration::of(hours: 2, seconds:59);
$durationB = Duration::fromNotation('P2WT3H', DurationNotation::Iso8601); //2 weeks and 3 hours
$durationC = Duration::fromDateInterval(new DateInterval('PT23M3S')); 


$duration = Duration::fromNotation('[10:00:00, 12:00:00)', DurationNotation::Iso800000);
// throws a Bakame\Tokei\InvalidDuration exception 
// because of the presence of the Y component

$durationB->hours;        // returns 1
$durationB->minutes;      // returns 1
$durationB->seconds;      // returns 1
$durationB->microseconds; // returns 234_000
$durationB->sign;         // returns 1
$durationB->daysCount;    // returns the absolute number of complete 24-hour days contained in the duration
$durationB->weeksCount;   // returns the absolute number of complete weeks contained in the duration
$durationB->isEmpty()     // returns true when the duration is zero, false otherhwise 

Duration::toNotation(DurationNotation $notation, Unit $unit): string
Duration::toDateInterval(): DateInterval
Duration::total(Unit $unit = Unit::Microseconds): float

[-]H:mm:ss[.microseconds]

$duration = Duration::of(hours: 25, seconds: 5); 
$duration->toNotation(DurationNotation::Iso8601); // returns 'P1D1H5S'

$duration = Duration::fromNotation('-P2W', DurationNotation::Iso8601); 
$duration->toNotation(DurationNotation::Iso8601); // returns '-P14D'

$duration = Duration::of(hours: 25, seconds: 5); 
$duration->toNotation(DurationNotation::Compact); // returns '1d 1h 5s'

$duration = Duration::of(microseconds: 3_661_234_000);
$duration->toDateInterval();          // returns DateInterval
$durationB->total(Unit::Microsecond); // returns the full duration in microseconds
$durationB->total(Unit::Hours);       // returns the full duration in hours

Duration::abs(): Duration
Duration::negated(): Duration
Duration::sum(Duration ...$duration): Duration
Duration::increment(int $weeks = 0, int $days = 0, int $hours = 0, int $minutes = 0, int $seconds = 0, int $microseconds = 0): Duration
Duration::multipliedBy(int $factor): Duration
Duration::dividedBy(int $factor): Duration
Duration::roundTo(Unit $precision, RoundingMode $roundingMode): Duration
Duration::clamp(Duration $min, Duration $max): Duration

$microseconds = 3_661_500_000;
$a = Duration::of(microseconds: $microseconds);
$b = $a->roundTo(Unit::Minute, RoundingMode::Ceil);
$c = $b->negate();
$d = $c->increment(minutes: -10);

echo $a->toNotation(DurationNotation::Chrono);                          // returns "1:01:01.500000"
echo $b->toNotation(DurationNotation::Chrono);                          // returns "1:01:00"
echo $c->toNotation(DurationNotation::Chrono);                          // returns "-1:01:00"
echo $c->abs()->toNotation(DurationNotation::Chrono);                   // returns "1:01:00"
echo $a->sum($b, $c, $d)->toNotation(DurationNotation::Chrono);         // returns "-0:09:58.500000"

$microseconds = 3_761_500_000;
$a = Duration::of(microseconds: $microseconds);
$a->toNotation(DurationNotation::Chrono);                           // returns "1:02:41.500000"
$a->roundTo(Unit::Minute, RoudingMode::Truncate)->toNotation(DurationNotation::Chrono); // returns "1:02:00"
$a->roundTo(Unit::Minute, RoudingMode::Round)->toNotation(DurationNotation::Chrono);    // returns "1:03:00"

Duration::compareTo(Duration $other): int;

$duration = Duration::of(microseconds: 3_661_500_000);
$other = Duration::fromNotation('PT1H1S', DurationNotation::Iso8601);

$duration->isShorterThan($other);        // returns false
$duration->isShorterThanOrEqual($other); // returns false
$duration->equals($other);               // returns false
$duration->isLongerThan($other);         // returns true
$duration->isLongerThanOrEqual($other);  // returns true

Interval::between(Time::midnight(), Time::at(10)); //represents 08:00 ≤ time < 10:00
Interval::between(Time::at(hour: 22), Time::at(hour: 6)); // represents 22:00 → 06:00 (next day)
Interval::collapsed(Time::midnight()); // represents 00:00:00/PT0S
Interval::circular(Time::midnight());  // represents  00:00:00/P1D

Interval::between(Time $start, Time $end): self;
Interval::since(Time $start, Duration $duration): self;
Interval::until(Time $end, Duration $duration): self;
Interval::around(Time $midRange, Duration $duration): self;
Interval::collapsed(Time $at): self;
Interval::circular(Time $at): self;
Interval::fromIso8601(string $notation): self
Interval::fromIso80000(string $notation): self
Interval::fromBourbaki(string $notation): self
Interval::fullDay(): self //a 24h-long instance starting at 00:00:00

$interval = Interval::between(Time::midnight(), Time::noon());
$interval->start;     // returns Time::midnight()
$interval->end,       // returns Time::noon()
$interval->duration;  // returns Duration::of(hours: 12);
$interval->type;      // returns IntervalType

enum IntervalType
{
    case Linear;    // returns true   (start < end)
    case Overflow;  // returns false  (start > end)
    case Circular;  // returns false  (start === end and duration is 'P1D')
    case Collapsed; // returns false  (start === end and duration is 'PT0S')
}

enum IntervalNotation
{
    case Iso8601StartDuration;
    case Iso8601DurationEnd;
    case Iso8601StartEnd;
    case Iso80000;
    case Bourbaki;
}

$interval = Interval::between(Time::midnight(), Time::noon());
$interval->toNotation(IntervalNotation::Iso8601StartDuration); // returns 00:00:00/PT12H
$interval->toNotation(IntervalNotation::Iso8601StartEnd);      // returns 00:00:00/12:00:00
$interval->toNotation(IntervalNotation::Iso8601DurationEnd);   // returns PT12H?00:00:00
$interval->toNotation(IntervalNotation::Iso80000);             // returns [00:00:00,12:00:00)
$interval->toNotation(IntervalNotation::Bourbaki);             // returns [00:00:00,12:00:00[

enum Bound
{
    case Start;
    case End;
}

Interval::steps(Duration $duration, Bound $from = Bound:Start): iterable<Time>
Interval::splitBy(Duration $duration, Bound $from = Bound:Start): IntervalSet
Interval::splitAt(Time ...$steps): IntervalSet

Interval::startingOn(Time $time): self
Interval::endingOn(Time $time): self
Interval::roundTo(Unit $precision, RoundingMode $roundingMode): self
Interval::expand(Duration $duration): self
Interval::shift(Duration $duration): self
Interval::shiftBound(Bound $from, Duration $duration): self
Interval::lasting(Bound $from, Duration $duration): self
Interval::complement(): self

Interval::equals(Interval $other): bool

Interval::compareDurationTo(Interval $other): int
Interval::sameDurationAs(Interval $other): bool
Interval::longerThan(Interval $other): bool
Interval::longerThanOrEqual(Interval $other): bool
Interval::shorterThan(Interval $other): bool
Interval::shorterThanOrEqual(Interval $other): bool

Interval:::contains(Interval $other): bool
Interval::overlaps(Interval $other): bool
Interval::abuts(Interval $other): bool
Interval::intersect(Interval $other): ?self
Interval::gap(Interval $other): ?self
Interval::union(Interval $other): IntervalSet
Interval::difference(Interval $other): IntervalSet

Interval::toNative(DateTimeInterface $reference): array
// returns array{startDate: DateTimeImmuable, interval: DateInterval}

use Bakame\Tokei\IntervalSet;

IntervalSet::__constrcut(Interval|IntervalSet ....$interval)

IntervalSet::duration(): Duration
IntervalSet::all(): list<Interval>
IntervalSet::first(): ?Interval
IntervalSet::last(): ?Interval
IntervalSet::nth(int $nth): ?Interval
IntervalSet::get(int $nth): Interval
IntervalSet::indexOf(Interval $interval): ?int
IntervalSet::LastIndexOf(Interval $interval): ?int
IntervalSet::has(Interval ...$intervals): bool
IntervalSet::isEmpty(): bool

IntervalSet::allFormatted(
    IntervalNotation $format = IntervalNotation::Iso8601StartDuration,
    ?Unit $unitOfDay = null,
): list<string> //all interval are converted to their Interval::format string representation

IntervalSet::allNative(DateTimeInterface $reference): array

IntervalSet::union(): IntervalSet 
IntervalSet::complement(): IntervalSet
IntervalSet::intersect(IntervalSet|Interval ...$others): IntervalSet
IntervalSet::difference(IntervalSet|Interval ...$others): IntervalSet
IntervalSet::gaps(): IntervalSet
IntervalSet::sorted(Bound $sortBound = Bound::Start, SortDirection|string $sortDirection = 'asc'): IntervalSet;

IntervalSet::any(callable $callback): bool
IntervalSet::every(callable $callback): bool
IntervalSet::each(callable $callback): bool
IntervalSet::map(callable $callback): iterable
IntervalSet::reduce(callable $callback, mixed $initial = null): mixed
IntervalSet::filter(callable $callback): IntervalSet
IntervalSet::sortedUsing(callable $callback): IntervalSet;
IntervalSet::push(IntervalSet|Interval ...$items): IntervalSet
IntervalSet::unshift(IntervalSet|Interval ...$items): IntervalSet
IntervalSet::remove(int ...$offset): IntervalSet
IntervalSet::replace(int $offset, Interval $newInterval): IntervalSet
IntervalSet::firstMatching(callable $callback): ?Interval
IntervalSet::lastMatching(callable $callback): ?Interval