sylius/calendar
Sylius Calendar Component provides date handling utilities for PHP applications. A lightweight package for working with dates and related calendar concerns in your project.
Installation
composer require sylius/calendar
Add to composer.json if using a monorepo or custom package structure.
Basic Usage
use Sylius\Component\Calendar\Calendar;
use Sylius\Component\Calendar\DateTimeFactory;
$dateTimeFactory = new DateTimeFactory();
$calendar = new Calendar($dateTimeFactory);
$dateTime = $calendar->createDateTime('2023-12-25 14:30:00');
First Use Case Parse and manipulate dates in a timezone-aware way:
$dateTime = $calendar->createDateTime('2023-12-25 14:30:00', 'Europe/Paris');
$dateTime->setTimezone(new \DateTimeZone('America/New_York'));
Timezone Handling
DateTime objects to avoid ambiguity:
$dateTime = $calendar->createDateTime('2023-12-25', 'UTC');
Calendar::convertTimezone() for conversions:
$converted = $calendar->convertTimezone($dateTime, 'Europe/London');
Date Arithmetic
DateTimeFactory for safe operations:
$nextWeek = $dateTimeFactory->addDays($dateTime, 7);
$nextMonth = $dateTimeFactory->addMonths($dateTime, 1);
Validation
$isValid = $calendar->isValidDateTime('2023-02-30'); // false
Integration with Laravel
$this->app->singleton(Calendar::class, function ($app) {
return new Calendar(new DateTimeFactory());
});
use Sylius\Component\Calendar\Calendar;
public function rules()
{
return [
'event_date' => ['required', function ($attribute, $value, $fail) {
$calendar = app(Calendar::class);
if (!$calendar->isValidDateTime($value)) {
$fail('The date is invalid.');
}
}],
];
}
Localization
$formatted = $calendar->formatDateTime($dateTime, 'Y-m-d H:i:s', 'en_US');
Timezone Assumptions
DateTime objects defaults to the system timezone, which can cause inconsistencies in multi-environment apps.$dateTime = $calendar->createDateTime('2023-12-25', 'UTC');
Date Arithmetic Edge Cases
2023-01-31 + 1 month → 2023-02-28 or 2023-02-31 depending on implementation). Sylius/Calendar handles this by adjusting to the last valid day.DateTimeFactory::addMonths() for predictable results:
$nextMonth = $dateTimeFactory->addMonths($dateTime, 1);
Immutable Objects
DateTime objects returned by DateTimeFactory are immutable. Modifications (e.g., setTimezone()) return a new object.$converted = $dateTime->setTimezone(new \DateTimeZone('UTC'));
Performance
DateTimeFactory methods for one-off operations; avoid instantiating Calendar repeatedly.Laravel Carbon Compatibility
Carbon, Sylius/Calendar’s DateTime is a wrapper around PHP’s DateTime. Convert between them carefully:
$carbon = Carbon::instance($dateTime->getDateTime());
$syliusDateTime = $calendar->createDateTime($carbon->toDateTimeString(), $carbon->getTimezone()->getName());
Custom DateTime Factories
DateTimeFactory to add domain-specific logic:
class BusinessDateTimeFactory extends DateTimeFactory
{
public function addBusinessDays(DateTimeInterface $dateTime, int $days): DateTimeInterface
{
// Custom logic for business days (e.g., skip weekends)
return parent::addDays($dateTime, $days);
}
}
Validation Rules
use Sylius\Component\Calendar\Calendar;
$calendar = app(Calendar::class);
$validator = Validator::make($data, [
'date' => ['required', function ($value) use ($calendar) {
return $calendar->isValidDateTime($value);
}],
]);
Event Scheduling
$eventDate = $calendar->createDateTime('2023-12-25', 'UTC');
event(new ScheduledEvent($eventDate));
Testing
DateTimeFactory for predictable test dates:
$mockFactory = Mockery::mock(DateTimeFactory::class);
$mockFactory->shouldReceive('createDateTime')
->with('2023-01-01', 'UTC')
->andReturn(new \DateTime('2023-01-01', new \DateTimeZone('UTC')));
$calendar = new Calendar($mockFactory);
How can I help you explore Laravel packages today?