Aeon, simple php DateTime library

without dependencies.

Aeon is a collection of libraries that are better way to handle DateTime in php. It provides a clean object oriented interface that is fully immutable, focused on improving the developer experience and making the implicit, explicit.

Installation
composer require aeon-php/calendar

Object Oriented API.

Fully immutable thanks to Psalm and PHPStan

<?php

use Aeon\Calendar\Gregorian\GregorianCalendar;
use Aeon\Calendar\TimeUnit;

$calendar = GregorianCalendar::UTC();
$calendar->currentYear()
    ->january()
    ->lastDay()
    ->noon()
    ->sub(TimeUnit::days(3));

Intuitive interface.

<?php

use Aeon\Calendar\Gregorian\Day;
use Aeon\Calendar\Gregorian\GregorianCalendar;
use Aeon\Calendar\Gregorian\DateTime;
use Aeon\Calendar\Gregorian\Month;
use Aeon\Calendar\Gregorian\Time;
use Aeon\Calendar\Gregorian\TimeZone;
use Aeon\Calendar\Gregorian\Year;

$dateTime = GregorianCalendar::UTC()->now();
$dateTime = DateTime::fromString('2020-01-01 00:00:00 UTC');
$dateTime = DateTime::fromDateTime(
    new DateTimeImmutable(
        '2020-01-01 00:00:00',
        new DateTimeZone('UTC')
    )
);
$dateTime = new DateTime(
    new Day(new Month(new Year(2020), 01), 01),
    new Time(00, 00, 00, 0),
    TimeZone::UTC()
);
$dateTime = DateTime::create(2020, 01, 01, 00, 00, 00, 0, 'UTC');

Time iterations.

  • Iterations forward and backward across time
  • Measure distance in time
<?php

use Aeon\Calendar\Gregorian\GregorianCalendar;
use Aeon\Calendar\Gregorian\TimePeriod;
use Aeon\Calendar\TimeUnit;

$now = GregorianCalendar::UTC()->now();

$now->until($now->add(TimeUnit::days(7)))
    ->iterate(TimeUnit::day())
    ->each(function(TimePeriod $timePeriod) {
        echo $timePeriod->start()
                ->day()
                ->format('Y-m-d H:i:s.uO') . "\n";
    });

Precise timestamps.

  • Support for leap seconds
  • GPS - with leap seconds
  • TAI - with leap seconds
  • UTC - with leap seconds
  • UNIX/POSIX - without leap seconds
<?php

use Aeon\Calendar\Gregorian\DateTime;
use Aeon\Calendar\Gregorian\TimeEpoch;

require_once __DIR__ . '../vendor/autoload.php';

$dateTime = DateTime::fromString('2020-01-01 00:00:00 UTC');
$timestampUNIX = $dateTime->timestamp(TimeEpoch::UNIX());
$timestampUTC = $dateTime->timestamp(TimeEpoch::UTC());
$timestampGPS = $dateTime->timestamp(TimeEpoch::GPS());
$timestampTAI = $dateTime->timestamp(TimeEpoch::TAI());

Retry Pattern.

  • Any time units
  • High precision
  • Different delay modifiers
<?php

use Aeon\Calendar\TimeUnit;
use Aeon\Retry\Execution;
use function Aeon\Retry\retry;

$result = retry(function (Execution $execution) {
    $random = \random_int(1, 3);

    if ($random === 2) {
        throw new \RuntimeException('exception');
    }

    return $random;
}, 3, TimeUnit::seconds(3));

Looking for more?

Documentation