PHP code example of yohang / calendr

1. Go to this page and download the library: Download yohang/calendr 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/ */

    

yohang / calendr example snippets




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) {
        // ...
    }
}



$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
}



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(); }
}



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),
        ];
    }
}



$manager = $calendar->getEventManager();
$manager->addProvider('my_source', new MyProvider());

$month = $calendar->getMonth(2025, 1);
$events = $manager->find($month);

foreach ($events as $event) {
    // ...
}