PHP code example of digital-craftsman / datetime-parts

1. Go to this page and download the library: Download digital-craftsman/datetime-parts 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/ */

    

digital-craftsman / datetime-parts example snippets


$nowInFacility = $now->setTimezone($facilityTimeZone);
$openFrom = $nowInFacility->setTime(
    (int) $facility->openFrom->format('H'),
    (int) $facility->openFrom->format('i'),
);

if ($nowInFacility < $openFrom) {
    throw new FacilityIsNotOpenYet();
}

if ($now->isBeforeInTimeZone($facility->openFrom, $facilityTimeZone)) {
    throw new FacilityIsNotOpenYet();
}

$now = Moment::fromString('2026-03-29 07:30:00');                       // UTC
$start = Moment::fromStringInTimeZone('2026-03-29 08:00', $berlin);    // converted to UTC

$date = $now->dateInTimeZone($berlin);   // Date
$time = $now->timeInTimeZone($berlin);   // Time
$month = $now->monthInTimeZone($berlin); // Month

$now->isBeforeInTimeZone($facility->openFrom, $facilityTimeZone);          // Time
$now->isBeforeInTimeZone($facility->earliestBookingDate, $facilityTimeZone); // Date
$now->isAfterInTimeZone($contract->lastMonth, $customerTimeZone);           // Month

$dueDate->isBefore($today);
$billingMonth->isEqualTo($currentMonth);
$date->isBetween($start, $end);

$endsAt->mustBeAfter($startsAt);
$now->mustNotBeBeforeInTimeZone(
    $earliestBookingDate,
    $timeZone,
    static fn () => new BookingNotPossibleYet(),
);

$bookingsAllowedFrom = $now->modifyInTimeZone('+7 days', $facilityTimeZone);

$date->add(CalendarPeriod::days(1));         // Warning: the result isn't used
$tomorrow = $date->add(CalendarPeriod::days(1));

$expiresAt = $now->add(Duration::fromHours(6));
$sameTimeTomorrow = $now->addInTimeZone(
    CalendarPeriod::days(1),
    $facilityTimeZone,
);

$dueDate = $invoiceDate->add(CalendarPeriod::days(14));
$nextBillingMonth = $billingMonth->add(CalendarPeriod::quarters(1));
$end = $start->add(Duration::fromMinutes(90));

$duration = $startedAt->durationUntil($endedAt);                       // Duration
$days = $startDate->periodUntil($endDate, CalendarUnit::DAY)->amount;  // int

$openingHours = new TimeRange(
    Time::fromString('08:00'),
    Time::fromString('20:00'),
);

$reservationTimeRange = $reservation->timeRangeInTimeZone($facilityTimeZone);
if ($openingHours->notContainsRange($reservationTimeRange)) {
    throw new ReservationIsOutsideOfOpeningHours();
}

$openingHours->contains(
    $reservationEnd,
    PeriodLimit::INCLUDING_START_AND_END,
);

$timeRange->mustNotStartBefore(Time::fromString('05:00'));
$timeRange->mustNotWrapAroundMidnight(
    static fn () => new TimeRangeMustBeWithinADay(),
);

$week = $date->week();          // e.g. 2026-W01 for 29.12.2025
$monday = $date->startOfWeek();

$recurrence = Recurrence::weekly(new Weekdays([
    Weekday::MONDAY,
    Weekday::WEDNESDAY,
]));

$nextReminderAt = $recurrence->nextOccurrenceAtTimeInTimeZone(
    $reminderTime,
    $now,
    $userTimeZone,
);
$lessonDates = $recurrence->occurrencesBetween(
    $seriesStart,
    $seriesEnd,
);

#[ORM\Entity]
class Facility
{
    #[ORM\Column(type: Moment::class)]
    public Moment $createdAt;

    #[ORM\Column(type: Time::class)]
    public Time $openFrom;

    #[ORM\Column(type: Date::class)]
    public Date $earliestBookingDate;
}

$now = $this->clock->now();

// In tests
$clock->freeze(Moment::fromString('2026-01-01 12:00:00'));