dipesh / nepali-date
Nepali Date 包是为处理尼泊尔日历而设计的。它提供了在英语(AD)和尼泊尔(BS)日历之间转换日期的功能,以及一系列处理和操作尼泊尔日期的方法。这个全面的工具使得尼泊尔日历系统的集成和操作变得无缝。
v2.1.0
2024-08-12 16:05 UTC
Requires
- php: >=8.0
- nesbot/carbon: ^2.0
Requires (Dev)
- laravel/pint: ^1.17
- phpunit/phpunit: ^11.3
README
Nepali Date 包是为处理尼泊尔日历而设计的。它提供了在英语(AD)和尼泊尔(BS)日历之间转换日期的功能,以及一系列处理和操作尼泊尔日期的方法。这个全面的工具使得尼泊尔日历系统的集成和操作变得无缝。
安装
要安装此包,请使用 Composer
composer require dipesh/nepali-date
使用
创建实例
use Dipesh\NepaliDate\NepaliDate; $date = new NepaliDate(language: 'np or en'); // Creates current date instance with provided language. //or $date = $date->setLang('np') or $date->setLang(new \Dipesh\NepaliDate\lang\Nepali()) // Creates immutable instance //or $date = new NepaliDate("2050-8-10") // Creates date instance with default language configuration //You are free to use either of these formats, e.g., yyyy-mm-dd or yyyy/mm/dd." //or $date = NepaliDate::make("2070-8-20"); // Creates date instance with default language configuration //or $date = NepaliDate::now(); // Creates current date instance // Work with global instance $date->create($date); // Creates an immutable date instance while retaining the previous configuration settings.
日期转换
$date->toAd(); // creates php Date instance //or $date = NepaliDate::fromADDate("1990-9-10");
根据语言配置获取日期组件
$date->year(); // Retrieves the year $date->month($format); // Retrieves the formatted month $date->day(); // Retrieves the day $date->weekDay($format); // Retrieves the formatted week day
日期操作和比较方法
$date->addDays($days); $date->subDays($days); $date->isEqual('2048/10/5'); // return true or false $date->isGreaterThan('2048/10/5'); $date->isLessThan('2048/10/5');
格式化
支持的格式字符:Y, m, M, F, d, w, D, l, g
$date->format('Y-m-d'); // 2050-10-8 $date->format('Y F d g l'); // 2050 Magh 8 Gate Sukrabar $date->format("Y-m-d, M d g l") // २०५०-१०-२८, माघ २८ गते बिहिबार" //or $date->format("Y-m-d, M d g l", 'np')
完整的 Calendar 系统推荐包
对于希望创建全面 Calendar 系统的开发者,我们推荐使用 dipesh/calendar 包。此包提供了一个易于使用的接口来管理完整的尼泊尔日历,允许您无缝地添加事件、浏览月份和年份,等等。
要安装包,请运行
composer require dipesh/calendar
扩展和定制尼泊尔日期包
此包具有出色的可扩展性,允许您定制关键组件以满足特定需求。该包围绕以下三个主要概念构建
- DateProcessor:处理所有与日期相关的计算和逻辑。
- Language:管理语言特定的方面,例如数字格式化和月份名称。
- Formatter:控制日期的格式化和显示方式。
您可以使用自己的实现来扩展或替换这些组件,从而允许您在不接触现有代码库的情况下修改核心逻辑。以下是如何实现此定制的示例
示例:扩展尼泊尔日期类
// Extending the main NepaliDate class class CustomDate extends \Dipesh\NepaliDate\NepaliDate { // Your new feature implementation goes here // Use a custom date processor for all date-related logic public function getDateProcessor() { return new CustomDateProcessor(); } // Use a custom formatter for all date formatting public function getFormatter() { return new CustomFormatter(); } }
示例:创建自定义 DateProcessor
// Implementing a custom DateProcessor class CustomDateProcessor implements \Dipesh\NepaliDate\Contracts\DateProcessor { public function getDays(int $year, int $month, int $day): int { // Your custom logic for calculating days } public function getDateFromDays(int $totalDays): string { // Your custom logic for calculating a date from total days } public function getWeekDayFromDays(int $days): int { // Your custom logic for determining the weekday from days } }
示例:创建自定义 Formatter
// Implementing a custom Formatter class CustomFormatter implements \Dipesh\NepaliDate\Contracts\Formatter { public function setUp(Date $date): static { // Setup logic with the date } public function format(string $format): string { // Your custom logic for formatting the date } public function formatNumber(int $number): string { // Your custom logic for formatting numbers } public function formatMonth(string $format = 'm'): mixed { // Your custom logic for formatting months } public function formatWeekDay(string $format = 'w'): mixed { // Your custom logic for formatting weekdays } }
示例:创建自定义 Language
// Implementing a custom language class CustomLanguage implements \Dipesh\NepaliDate\Contracts\Language { public function getGate(): string { // Your custom language specific gate especially useful for nepali language and you might not need this // Eg: return ""; } public function getDigit(int $digit):int|string { // Your custom language specific digit // Eg: return 1; } public function getWeek(int $week):array { // Your custom language specific week day // Eg: return ['l' => 'Sunday', 'D' => 'Sun']; } public function getMonth(int $month):array { // Your custom language specific month // Eg: return ['F' => 'January', 'M' => 'Jan']; } }
许可证
Nepali Date 是一个开源包,根据 MIT 许可证 许可。****