hill-valley/fluxcap

Set of date-time related immutable classes

1.x-dev 2024-02-09 22:16 UTC

This package is auto-updated.

Last update: 2024-10-09 23:38:42 UTC


README

This package contains:

  • immutable classes DateTime, Date and Time (wrapping \DateTimeImmutable)
  • immutable class TimeZone (wrapping \DateTimeZone)
  • immutable class Duration (wrapping \DateInterval)
  • enums Month and Weekday

Latest release PHP version requirement Psalm type coverage License

Installation

composer require hill-valley/fluxcap

Examples

DateTime, Date, Time

use HillValley\Fluxcap\DateTime;
use HillValley\Fluxcap\Date;
use HillValley\Fluxcap\Time;
use HillValley\Fluxcap\TimeZone;

$dateTime = DateTime::now();
$dateTime = DateTime::fromString('2015-10-21 09:30:00');
$dateTime = DateTime::fromString('2015-10-21 09:30:00', TimeZone::fromString('Europe/Berlin'));
$dateTime = DateTime::fromFormat('d.m.Y H.i', '21.10.2015 09.30');
$dateTime = DateTime::fromParts(2015, 10, 21, 9, 30, 0);
$dateTime = DateTime::fromNative(new \DateTimeImmutable());

$date = Date::today();
$date = Date::fromString('2015-10-21');

$time = Time::now();
$time = Time::fromString('09:30:00');

$dateTime->getYear();
$dateTime->toIso(); // 2015-10-21T09:30:00.000000+02:00
$dateTime->format('d.m.Y, H:i');
$dateTime->formatIntl(\IntlDateFormatter::MEDIUM); // requires intl extension

$dateTime2 = $dateTime->addDays(3);
$date2 = $date->toLastDayOfMonth();
$date2 = $date->toFirstDayOfQuarter();

$dateTime->isPast();
$dateTime->equals($dateTime2);
$dateTime->lowerEquals($dateTime2);
$duration = $dateTime->diff($dateTime2);

Duration

use HillValley\Fluxcap\Duration;

$duration = Duration::fromString('P2DT5H');
$duration = Duration::fromParts(days: 2, hours: 5);
$duration = Duration::years(2);

$duration->toIso(); // P2DT5H
$duration->format('%d days');
$duration->getHours();

Month, Weekday

use HillValley\Fluxcap\Month;
use HillValley\Fluxcap\Weekday;

$month = Month::October;

$weekdays = Weekday::cases();
$weekday = Weekday::Tuesday;

// same methods for months

$weekday->getName();
$weekday->getAbbreviation();

$weekday->getIntlName(); // via intl extension
$weekday->getIntlAbbreviation();

$count = $weekday->diffToNext(Weekday::Friday);