devhelp / calendar
1.1
2013-06-20 19:47 UTC
Requires
- php: >=5.3
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-14 14:06:02 UTC
README
鸣谢
插件由 Devhelp.pl 提供: (http://devhelp.pl)
目的
Devhelp/Calendar 包的目的是帮助创建易于定义事件的日历,这些事件既发生在固定日期,也发生在从其他事件(或使用自定义逻辑计算)引用的日期。
它还有助于定义与特定日期、事件相关或使用自定义逻辑计算的休息日。
这适合您吗?
- 您想要定义包含事件的日历吗?
- 您想要存储相互关联的事件吗?
- 您想要定义根据年份或其他事件在不同日期发生的事件吗?
- 您想要有一个轻松计算休息日的方法吗?
如果以上任何一个答案是“是”,那么我希望您会喜欢使用这个包。
安装
Composer
将包添加到 composer.json
"require" : {
"devhelp/calendar": "dev-master"
}
运行更新
composer update devhelp/calendar
使用方法
示例日历定义
$definition = array(
Calendar::DAYS_OFF => array(
new Day(3, 1),
new Day(4, 1),
new Day(5, 1),
new DayRange(new Day(3, 1), new Day(10, 1)),
new DayReference('national/constitution-day'),
),
Calendar::EVENTS => array(
'religion' => array(
'easter' => function (Calendar $calendar, $year) {
$a = $year % 19;
$b = intval($year / 100);
$c = $year % 100;
$d = intval($b / 4);
$e = $b % 4;
$f = intval(($b + 8) / 25);
$g = intval(($b - $f + 1) / 3);
$h = (19 * $a + $b - $d - $g + 15) % 30;
$i = intval($c / 4);
$k = $c % 4;
$l = (32 + 2 * $e + 2 * $i - $h - $k) % 7;
$m = intval(($a + 11 * $h + 22 * $l) / 451);
$p = ($h + $l - 7 * $m + 114) % 31;
$day = $p + 1;
$month = intval(($h + $l - 7 * $m + 114) / 31);
return new Day($day, $month);
},
'divine-mercy-sunday' => new DayReference('religion/easter', '+7 day'),
),
'national' => array(
'independence-day' => new Day(11, 11),
'constitution-day' => new Day(3, 5),
'joining-european-union' => new DayReference('national/constitution-day')
),
'birthday' => array(
'joanne' => new Day(23, 3),
'paul' => new Day(31, 8),
'margaret' => new DayReference('birthday/paul', '+2 day'),
),
'specific-day' => array(
'first-day-of-march' => new Day(1, 3),
'last-day-of-february' => new DayReference('specific-day/first-day-of-march', '-1 day'),
'7-august' => new Day(7, 8),
'7-september' => new DayReference('specific-day/7-august', '+1 month'),
)
)
);
$calendar = new \Devhelp\Calendar\Calendar($definition);
获取事件日期
对于通过引用(值不为 0)或自定义函数定义的事件,您需要指定年份来进行计算
$calendar->getDay('religion/easter', 2013);
对于通过简单引用或定义为日期的事件,只需传递事件名称
$calendar->getDay('birthday/joanne');
$calendar->getDay('national/joining-european-union');
获取休息日
您可以获取特定年份在日历中定义的休息日
$calendar->getDaysOff(2013);
不用担心重复的日期。此函数将仅返回唯一的日期
如果计算休息日不需要进行数学运算,则无需传递年份