首页
/ When 项目技术文档

When 项目技术文档

2024-12-20 09:48:30作者:郦嵘贵Just

本文档将详细介绍如何安装和使用 When 项目,该项目是一个 PHP 7.1+ 的日期/日历递归库,支持 RFC5455 重复规则。

1. 安装指南

When 项目的安装非常简单,您可以通过以下命令使用 Composer 进行安装:

composer require tplaner/When

确保您的环境已经安装了 Composer。

2. 项目使用说明

When 项目提供了对 RFC5455 重复规则特性的完整支持。以下是一些基本使用示例:

  • 生成下五个 13 号星期五的日期:
$r = new When();
$r->startDate(new DateTime("19980213T090000"))
  ->freq("monthly")
  ->count(5)
  ->byday("fr")
  ->bymonthday(13)
  ->generateOccurrences();

print_r($r->occurrences);
  • 使用重复规则(rrule)生成下五个 13 号星期五的日期:
$r = new When();
$r->startDate(new DateTime("19980213T090000"))
  ->rrule("FREQ=MONTHLY;BYDAY=FR;BYMONTHDAY=13;COUNT=5")
  ->generateOccurrences();

print_r($r->occurrences);
  • 跳过已知的星期五 13 号日期:
$r = new When();
$r->startDate(new DateTime("19980213T090000"))
  ->freq("monthly")
  ->count(5)
  ->byday("fr")
  ->bymonthday(13)
  ->exclusions('19990813T090000,20001013T090000')
  ->generateOccurrences();

print_r($r->occurrences);
  • 生成 2018 年内所有星期五 13 号的日期:
$r = new When();
$r->startDate(new DateTime("19980213T090000"))
  ->rrule("FREQ=MONTHLY;BYDAY=FR;BYMONTHDAY=13");

$occurrences = $r->getOccurrencesBetween(new DateTime('2018-01-01 09:00:00'),
                                         new DateTime('2019-01-01 09:00:00'));
print_r($occurrences);

3. 项目 API 使用文档

When 项目的主要类和方法如下:

  • When: 当前的 When 实例。
  • startDate(DateTime $date): 设置开始日期。
  • freq(string $frequency): 设置重复频率。
  • count(int $count): 设置生成事件的最大次数。
  • byday(string $day): 设置星期几重复。
  • bymonthday(int $day): 设置月份中的哪一天重复。
  • exclusions(string $dates): 设置排除的日期。
  • generateOccurrences(): 生成事件日期。
  • getOccurrencesBetween(DateTime $start, DateTime $end): 获取两个日期之间的所有事件。

更多方法和细节请参考项目文档和示例代码。

4. 项目安装方式

项目安装方式已在“安装指南”部分详细说明,通过 Composer 安装:

composer require tplaner/When

确保您的开发环境已经配置好了 PHP 和 Composer。

登录后查看全文
热门项目推荐