Search by

kodhe / calendar

karyakode

Kodhe calendar library

Package info

github.com/kodhe/calendar

pkg:composer/kodhe/calendar

Statistics

Installs: 3

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.1.0 2026-09-28 18:23 UTC

This package is not auto-updated.

Last update: 2026-10-28 10:32:43 UTC


README

A refactored version of the CodeIgniter 3 Calendar library under the Kodhe\Framework\Calendar namespace. It renders monthly calendar views (HTML table or JSON), supports prev/next navigation, long/short day and month names, Sunday/Monday week start, showing days from other months, and inserting event links per date.

Installation

composer require kodhe/calendar

Requirements: PHP >= 8.1. No external dependencies.

Quick Start

<?php

declare(strict_types=1);

require __DIR__ . '/vendor/autoload.php';

use Kodhe\Framework\Calendar\Calendar;

$calendar = new Calendar();

echo $calendar->generate(2024, 1);
// <table class="calendar"> ... </table>

With navigation and event data (date => URL):

$calendar = new Calendar([
    'show_next_prev' => true,
    'next_prev_url'  => '/calendar/',
]);

$data = [
    1  => '/events/new-year',
    15 => '/events/meeting',
    25 => '/events/workshop',
];

echo $calendar->generate(2024, 1, $data);

Configuration

The configuration array is accepted via the constructor and is idempotent per instance:

Key Type Default Description
start_day string sunday First day of the week: sunday / monday
month_type string long Month name style: long (January) or abr (Jan)
day_type string abr Day name style: long or abr
locale string en Locale for day/month names
template array|null null Custom CSS template (based on default_template())
show_next_prev bool false Show previous/next month links
next_prev_url string '' Base URL for the navigation links
show_other_days bool false Show dates from the previous/next month
local_time int|null null Reference timestamp for "now" (null = system time)

Example calendar starting on Monday with short names:

$calendar = new Calendar([
    'start_day'       => 'monday',
    'show_other_days' => true,
    'month_type'      => 'abr',
]);

echo $calendar->generate(2024, 1);

Main Methods

generate(int|string $year = '', int|string $month = '', array $data = []): string

Builds the calendar HTML for a given month/year. Empty parameters default to the current month/year. $data maps day numbers to event URLs ([15 => '/event']); non-array values for a day are also rendered as-is.

asJson(int|string $year = '', int|string $month = '', array $data = []): string

Returns the calendar as JSON — useful for dynamic calendars on the frontend.

setRenderer(CalendarRendererInterface $renderer): self

Replaces the renderer at runtime (e.g. a custom renderer for Tailwind markup).

Utility helpers (CI3-compatible + camelCase variants)

$calendar->get_month_name(1);        // "January"  (alias: getMonthName)
$calendar->get_day_names('abr');     // ['Sunday','Monday',...] short (alias: getDayNames)
$calendar->adjust_date(12, 2024);    // [1, 2025]  (alias: adjustDate)
$calendar->get_total_days(2, 2024);  // 29         (alias: getTotalDays)
$calendar->get_last_day(2, 2024);    // 4 (day index) (alias: getLastDay)
$calendar->get_total_weeks(2, 2024); // 5          (alias: getTotalWeeks)

Struktur Direktori

src/
├── Calendar.php                  # Main class (CI3-compatible API)
├── Contracts/                    # CalendarInterface, CalendarRendererInterface
├── Generators/MonthGenerator.php # Calendar cell generation logic
├── Renderers/                    # HtmlTableRenderer, JsonRenderer
├── Traits/                       # ConfigurableTrait, SingletonTrait
├── ValueObjects/                 # CalendarDate, CalendarEvent
└── helpers.php                   # days_in_month(), etc.

Migration Notes from the Old Namespace

Older documentation/test versions used Kodhe\Library\Calendar\Calendar. For modern Composer installations, use:

use Kodhe\Framework\Calendar\Calendar;

The PSR-4 mapping Kodhe\Framework\Calendar\ → calendar/src/ is declared in calendar/composer.json.