定制 kindy/hijridate 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

kindy/hijridate

Composer 安装命令:

composer require kindy/hijridate

包简介

Package for Hijri date conversion and manipulation in PHP

README 文档

README

This PHP package provides functionalities for handling Hijri (Islamic) dates, including conversion, formatting, and integration with Gregorian calendars. The package features enhanced modularity and functionality, making it highly useful for PHP developers requiring Hijri date processing.

Features

  • Hijri Calendar Compliance:

    • Fully adheres to the astronomical conventions used by astronomers and matches the standards in Microsoft products and the Calendar of the Centuries by Dr. Saleh Al-Ojairi.
    • Includes full support for the Umm Al-Qura calendar.
  • Custom Calendar Adjustments:

    • Supports saving and restoring calendar adjustments, allowing customization to reflect specific events like Ramadan and Eid declarations.
  • Hijri and Gregorian Integration:

    • Combines both calendars seamlessly in a single programming operation.
    • Uses date-style format characters prefixed with _ for Hijri date formatting.
  • Language Support:

    • Includes 20 global languages for Hijri month names.
    • Displays Gregorian dates in Arabic when the language is set to Arabic.
  • Integration with PHP DateTime:

    • Fully compatible with PHP's DateTime class, with additional methods for Hijri-specific functionalities.
    • Automatically handles timezone differences.

Installation

You can install the package via Composer:

composer require kindy/hijridate

Namespace and Class Structure

The package follows a modular approach with the following structure:

  • Namespace: Kindy\HijriDate
  • Classes:
    • HijriDateTime: Handles Hijri date and time functionalities.
    • Calendar: Provides core conversion and calculation utilities.
    • CalendarAdjustment: Enables custom calendar adjustments.

Usage

Basic Example: Display Current Hijri Date

HijriDateTime Class

Constructor

public HijriDateTime::__construct(
    string $time = "now",
    DateTimeZone $timezone = null,
    string $langcode = null,
    Kindy\HijriDate\Calendar $calendar = null
)
  • time : String in a format accepted by strtotime() default is 'now'
  • $timezone : Time zone of the time default is ini timezone
  • langcode: Default language (ar for Arabic, others default to English).
  • calendar: Calendar object which used for calendar converting, if not set the class will create Calendar object with default settings.

Format Hijri Dates

public function format(string $format): string

Format Hijri Dates

public function format(string $format): string

Hijri Dates Static Method

HijriDateTime::Date('_Y-_m-_d','2024-12-3');

Hijri Dates objet by new instance

use Kindy\HijriDate\HijriDateTime;

echo (new HijriDateTime())->format('_j _M _Yهـ');
// Output: 8 شعبان 1436هـ

Hijri and Gregorian Dates Together

echo (new HijriDateTime())->format('D _j _M _Yهـ (j-m-Yم)');
// Output: الثلاثاء 8 شعبان 1436هـ (26-05-2015م)

Convert Specific Gregorian Date to Hijri

echo (new HijriDateTime("2015-5-22"))->format('D _j _M _Yهـ (j-m-Yم)');
// Output: الجمعة 4 شعبان 1436هـ (22-05-2015م)

Create Hijri Date Object from Hijri Date

use Kindy\HijriDate\HijriDateTime;

$hijirDate = HijriDateTime::createFromHijri(1436, 9, 1);
echo $hijirDate->format('D _j _M _Yهـ (j-m-Yم)');
// Output: الخميس 1 رمضان 1436هـ (18-06-2015م)

Formatting Time and Date in the Hijri Calendar

The function formats time and date using specific characters, with no differences in parameters but varying outputs depending on the characters used. Below is a table explaining the functionality of each character:

Character Description Example Output
_j Day without leading zeros 1-30
_d Day with leading zeros 01-30
S Ordinal suffix (English only, new in version 2.3) st-nd-th
_z Day of the year (starting from zero) 0-354
_M, _F Month name محرم, صفر, ...
_m Month number with leading zeros 01-12
_n Month number without leading zeros 1-12
_t Number of days in the month 29-30
_L Leap year indicator (1 = Leap Year, 0 = Not Leap Year) 1 or 0
_Y Full year number 1436
_y Two-digit year 36

Adjusted Gregorian Date Formatting in Arabic Language

When selecting the Arabic language, some Gregorian date formatting characters are adjusted as follows:

Character Description Example Output
l, D Day of the week name السبت, الأحد...
F Month names in Syriac terms كانون الثاني, شباط...
M Month names in English terms يناير, فبراير...
a AM/PM in Arabic symbols ص-م
A AM/PM in Arabic full text صباحا - مساء

Extended Features

Additional Methods

The HijriDateTime class includes custom methods for Hijri-specific functionalities:

  • HijriDateTime::createFromHijri(year, month, day): Creates a HijriDateTime object from a specific Hijri date.

  • HijriDateTime::setDateHijri(year, month, day): Sets the Hijri date for an existing HijriDateTime object.

Calendar Class

The Calendar class provides low-level utilities such as:

  • Conversion between Julian and Hijri calendars.
  • Determining leap years.
  • Validating Hijri dates.

Example: Days in Hijri Month

use Kindy\HijriDate\Calendar;

echo (new Calendar())->days_in_month(9, 1436);
// Output: 29

Using the Package with Legacy Code

If you want to use this library without changing existing DateTime-dependent code, alias HijriDateTime as DateTime:

use Kindy\HijriDate\HijriDateTime as DateTime;

Alternatively, create a custom hdate() function to replace PHP's date():

function hdate($format, $timestamp = null)
{
    if (!isset($timestamp)) {
        $timestamp = time();
    }
    $d = new HijriDateTime('@' . $timestamp);
    return $d->format($format);
}

Contributing

Your contribution is welcome

Credits

License

This package is licensed under GPL-2 License.

Note: For detailed documentation and examples, refer to the official API or the examples.php file in the repository.

kindy/hijridate 适用场景与选型建议

kindy/hijridate 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.38k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2024 年 12 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-03