yohang / calendr
Object Oriented calendar management
Installs: 604 816
Dependents: 3
Suggesters: 0
Security: 0
Stars: 463
Watchers: 24
Forks: 65
Open Issues: 11
pkg:composer/yohang/calendr
Requires
- php: >=8.2
Requires (Dev)
- doctrine/doctrine-bundle: >=2.7.2,<4.0
- doctrine/orm: ^2.9.3|^3.0.0
- friendsofphp/php-cs-fixer: ^3.90
- infection/infection: ^0.31.9
- phpspec/prophecy-phpunit: ^2.3.0
- phpunit/phpunit: ^11.5.44
- psr/simple-cache: >=0.2.0,<4
- rector/rector: ^2.2
- symfony/browser-kit: >=6.4,<9.0
- symfony/config: >=4.4,<9.0
- symfony/css-selector: >=6.4,<9.0
- symfony/dependency-injection: >=4.4,<9.0
- symfony/framework-bundle: >=6.4,<9.0
- symfony/http-kernel: >=5.4,<9.0
- symfony/twig-bundle: >=4.4,<9.0
- symfony/var-dumper: >=4.4,<9.0
- twig/twig: ^3.3.2
- vimeo/psalm: ^6.13@dev
- dev-main
- dev-master / 4.0.x-dev
- 4.0.0
- 3.0.x-dev
- 3.0.0
- 2.2.x-dev
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.0
- 1.1.x-dev
- 1.1.1
- 1.1.0
- 1.0.x-dev
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 1.0.0-beta4
- 1.0.0-beta3
- 1.0.0-beta2
- 1.0.0-beta
- dev-feature/enhance-twig-extension-and-calendar-view
- dev-tests/add-functional-test
- dev-chore/rename-collection-basic-classes
- dev-feature/remove-abstract-event-and-uid
- dev-refactor/modernize-codebase
- dev-ci/migrate-to-github-workflow
This package is auto-updated.
Last update: 2025-12-02 20:13:34 UTC
README
A modern, object-oriented calendar management library for PHP 8.2+.
CalendR provides a clean, immutable, and iterable API to manipulate time periods (Years, Months, Weeks, Days...) and manage associated events.
✨ Features
- Object-Oriented Periods: Manipulate
Year,Month,Week,Dayas objects, not strings or timestamps. - Fully Iterable: Iterate over a Year to get Months, or a Month to get Days, using native
foreachloops. - Immutable by Design: Based on
DateTimeImmutable, ensuring safe date manipulations. - Event Management: Fetch and aggregate events from multiple sources (Doctrine, API, etc.) for any period.
- Zero Dependencies: The core library has no external dependencies.
- Framework Integrations: Includes a Symfony Bundle and Twig extensions.
📦 Installation
composer require yohang/calendr
🚀 Usage
1. Navigating Time
The Calendar class is your main entry point. It acts as a factory to create periods configured with your preferences (e.g., first day of the week).
<?php use CalendR\Calendar; $calendar = new Calendar(); // Get a specific year $year = $calendar->getYear(2025); foreach ($year as $month) { echo $month->format('F Y') . "\n"; // Iterate over days in that month foreach ($month as $day) { // ... } }
2. Working with Periods
Every period object implements PeriodInterface, providing powerful methods:
<?php $month = $calendar->getMonth(2025, 1); // January 2025 // Check containment if ($month->contains(new \DateTimeImmutable('2025-01-15'))) { echo "We are in the middle of the month!"; } // Navigation $nextMonth = $month->getNext(); // February 2025 $prevMonth = $month->getPrevious(); // December 2024 // DatePeriod compatibility foreach ($month->getDatePeriod() as $date) { // $date is a DateTimeImmutable }
3. Managing Events
CalendR can attach events to any period. You need to configure an EventManager with one or more ProviderInterface.
Implement your Event
<?php use CalendR\Event\AbstractEvent; class MyEvent extends AbstractEvent { public function __construct( private \DateTimeImmutable $begin, private \DateTimeImmutable $end ) {} public function getBegin(): \DateTimeInterface { return $this->begin; } public function getEnd(): \DateTimeInterface { return $this->end; } public function getUid(): string { return uniqid(); } }
Create a Provider
<?php use CalendR\Event\Provider\ProviderInterface; class MyProvider implements ProviderInterface { public function getEvents(\DateTimeInterface $begin, \DateTimeInterface $end, array $options = []): array { // Query your database or API here using $begin and $end return [ new MyEvent($begin, $end), ]; } }
Retrieve Events
<?php $manager = $calendar->getEventManager(); $manager->addProvider('my_source', new MyProvider()); $month = $calendar->getMonth(2025, 1); $events = $manager->find($month); foreach ($events as $event) { // ... }
🧩 Symfony & Twig Integration
If you use Symfony, the bundle is automatically configured.
# config/packages/calendr.yaml calendr: periods: default_first_weekday: 1 # Monday
Twig Functions
You can access periods directly in your templates:
{# Iterate over days of the current month #} {% set month = calendr_month(2025, 1) %} <table> {% for week in month %} <tr> {% for day in week %} <td> {{ day.begin|date('d') }} {# Fetch events for this specific day #} {% for event in calendr_events(day) %} <span class="event">{{ event.uid }}</span> {% endfor %} </td> {% endfor %} </tr> {% endfor %} </table>