维护者

详细信息

github.com/Pause-Inc/When

源代码

dev-master 2013-07-09 15:33 UTC

This package is not auto-updated.

Last update: 2024-09-24 03:24:52 UTC


README

Build Status

PHP日期递归库

作者:Thomas Planer

### 关于 经过全面搜索,我没有找到可以处理递归日期的PHP库。然而,存在一个:http://phpicalendar.org/,但是从应用程序中提取递归部分非常困难。

奇怪的是,Ruby和Python都有非常优秀的日期递归库

Ruby: http://github.com/seejohnrun/ice_cube

Python: http://labix.org/python-dateutil

由于我没有找到PHP的等效库,因此创建了When

### 单元测试

测试是用PHPUnit编写的(http://www.phpunit.de/

初始测试集是从RFC5545中找到的示例创建的(http://tools.ietf.org/html/rfc5545)。

### 文档

初始化类

$when = new Recurrence();

初始化类后,可以通过调用recur方法创建一个周期性事件

$when->recur(<DateTime object|valid Date string>, <yearly|monthly|weekly|daily>);

您可以通过指定limit()来限制要查找的日期数量

$when->limit(<int>);

或者,您可以指定一个结束日期

$when->until(<DateTime object|valid Date String>);

注意:结束日期不必与递归模式匹配。

注意:当达到限制或结束日期时,脚本将停止返回结果。

更多文档即将推出,请查看单元测试以了解该类的能力。

### 示例(请查看单元测试以获取更多示例)

下一个5个星期五13号的出现

$r = new Recurrence();
$r->recur(new DateTime(), 'monthly')
  ->count(5)
  ->byday(array('FR'))
  ->bymonthday(array(13));

while($result = $r->next())
{
	echo $result->format('c') . '<br />';
}

每隔四年,11月第一个星期一的下一个星期二,接下来20年(美国总统选举日)

// this is the next election date
$start = new DateTime('2012-09-06');

$r = new Recurrence();
$r->recur($start, 'yearly')
  ->until($start->modify('+20 years'))
  ->interval(4)
  ->bymonth(array(11))
  ->byday(array('TU'))
  ->bymonthday(array(2,3,4,5,6,7,8));

while($result = $r->next())
{
	echo $result->format('c') . '<br />';
}

您现在可以将原始RRULE传递给该类

$r = new Recurrence();
$r->recur('19970922T090000')->rrule('FREQ=MONTHLY;COUNT=6;BYDAY=-2MO');

while($result = $r->next())
{
	echo $result->format('c') . '<br />';
}

警告

  • 如果您提交的没有结果的模式,脚本将无限循环。
  • 如果您没有指定模式的上限(直到)或计数,您必须在脚本中限制结果的数目以避免无限循环。

### 贡献

如果您想贡献,请创建一个分支,在做出更改后提交一个pull请求。

请在提交pull请求之前确保单元测试通过100%。

目前有78个测试,1410个断言。

>>>phpunit --verbose tests
PHPUnit 3.4.15 by Sebastian Bergmann.

tests
 When_Core_Tests
 ..

 When_Daily_Rrule_Test
 .....

 When_Daily_Test
 .....

 When_Iterator_Tests
 ..

 When_Monthly_Rrule_Test
 ..............

 When_Monthly_Test
 ..............

 When_Weekly_Rrule_Test
 ........

 When_Weekly_Test
 ........

 When_Rrule_Test
 ..........

 When_Yearly_Test
 ..........

Time: 2 seconds, Memory: 6.00Mb

OK (78 tests, 1410 assertions)