yohang/calendr 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yohang/calendr

Composer 安装命令:

composer require yohang/calendr

包简介

Object Oriented calendar management

README 文档

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.

CI Status Coverage Status Mutation testing badge

✨ Features

  • Object-Oriented Periods: Manipulate Year, Month, Week, Day as objects, not strings or timestamps.
  • Fully Iterable: Iterate over a Year to get Months, or a Month to get Days, using native foreach loops.
  • 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>

yohang/calendr 适用场景与选型建议

yohang/calendr 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 621.34k 次下载、GitHub Stars 达 465, 最近一次更新时间为 2012 年 01 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「date」 「calendar」 「events」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 yohang/calendr 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 yohang/calendr 我们能提供哪些服务?
定制开发 / 二次开发

基于 yohang/calendr 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 621.34k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 473
  • 点击次数: 23
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 465
  • Watchers: 24
  • Forks: 67
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2012-01-20