avenirer / php-calendar
用于生成日历的PHP类。
Requires
- php: >=7
This package is auto-updated.
Last update: 2024-09-07 01:48:23 UTC
README
使用示例
初始化类
$calendar = new Calendar;
然后简单链接设置
$returned = $calendar ->setLocale('ro') ->addEvents($events) ->weekStartsMonday() ->padWithZeros() ->showMonthName() ->addDayCustomLink('/test/{year}/{month}/{day}') ->getMonth('2022-11-01') ->asMatrix();
设置日历的最小要求是
$returned = $calendar->getMonth('2022-11-01')->asMatrix();
如果您想有更多设置,您可以在使用 getMonth()
方法之前使用方法的链接。
让我们解释...
设置语言
我们可以使用 setLocale()
方法设置语言。如果我们想使用英语,我们可以忽略这个方法。否则,您可以选择 'fr'(法语)和 'ro'(罗马尼亚语)。
向日历添加事件
如果您认为这很有用,可以向日历添加事件。为了添加事件,您应该创建一个事件对象
$events = new Events; $events->add( [ 'startDate' => '2022-11-22', 'startTime' => '13:10', 'title' => 'Event title', ], );
通过传递一个包含事件详细信息的数组给 add() 方法,您可以使用该方法添加事件详情。事件的必需字段是 startDate
和 title
。如果需要,数组还可以接收其他参数
[ 'startDate' => '2022-11-22', // start date of the event 'startTime' => '13:10', // start time of the event 'endDate' => '2022-12-01', // end date of the event 'endTime' => '14:00', // end time of the event 'title' => 'Event title', // the event title 'shortContent' => 'The teaser of the content', // a short description of the event 'content' => 'Full description of the event', // a longer description of the event 'link' => 'https://link.to/wherever' // link to the event page ]
顺便说一下,您可以在同一个方法中添加多个事件
$events->add( [ [ 'startDate' => '2022-11-22', 'startTime' => '13:10', 'title' => 'Event title', ], [ 'startDate' => '2022-12-22', 'startTime' => '13:40', 'title' => 'One Event title', ], ] );
周是从星期一开始吗?
如果您想将周设置为从星期一开始,就像“不是美国式”一样,您可以使用 weekStartsMonday()
方法。
设置点击日时使用的链接
如果您想进一步使用日历,并使其具有交互性,允许用户点击天数并跳转到特定URL,您可以使用 addDayCustomLink()
方法,其中 {year}
、{month}
和 {day}
分别作为值占位符。
我们谈论的是哪个月份?
为了检索月份,我们使用 getMonth()
方法,使用 Y-m-d
格式指定年份和月份。不用担心日期;将创建整个月份。
我们希望返回什么?
矩阵
如果您想以矩阵(HTML表格)的形式查看月份,可以将此作为 getMonth()
方法的第二个参数传递 ...->getMonth('2022-11-01', 'matrix')
。此外,您还可以在 getMonth()
之后使用另一个方法 asMatrix()
。
列表
如果您想以列表(HTML UL列表)的形式查看月份,可以将此作为 getMonth()
方法的第二个参数传递 ...->getMonth('2022-11-01', 'list')
。此外,您还可以在 getMonth()
之后使用另一个方法 asList()
。
显示月份名称吗?
对于输出HTML的月份的方法,我们可以请求使用 showMonthName()
方法显示月份名称。
显示前导零吗?
对于一位数的月份天数(1到9),您可以决定在日期号码前添加前导零。您可以通过添加 padWithZeros()
方法来实现这一点。
数组
如果您只想有一个包含月份的数组,可以将此作为 getMonth()
方法的第二个参数传递 ...->getMonth('2022-11-01', 'array')
。此外,您还可以在 getMonth()
之后使用另一个方法 asArray()
。