是否要创建在线日历,以便您可以在iPhone的日历应用程序或Google日历上显示它们?这可以通过以iCalendar格式(RFC 5545)生成日历来完成,这是一种文本格式,可以通过不同的应用程序加载。
此类日历的格式是在RFC 5545中定义的,这不是一个令人愉快的阅读体验。该软件包实现了RFC 5545和RFC 7986的一些扩展,为您提供了易于使用的API来创建日历。我们的意图不是完全实施这些RFC,而是提供易于使用的直接API。
这是如何使用它的示例:
use Spatie IcalendarGenerator Components Calendar ;
use Spatie IcalendarGenerator Components Event ;
Calendar :: create ( ' Laracon online ' )
-> event ( Event :: create ( ' Creating calender feeds ' )
-> startsAt ( new DateTime ( ' 6 March 2019 15:00 ' ))
-> endsAt ( new DateTime ( ' 6 March 2019 16:00 ' ))
)
-> get ();以上代码将生成此字符串:
BEGIN:VCALENDAR
VERSION:2.0
PRODID:spatie/icalendar-generator
NAME:Laracon online
X-WR-CALNAME:Laracon online
BEGIN:VEVENT
UID:5ef5c3f64cb2c
DTSTAMP;TZID=UTC:20200626T094630
SUMMARY:Creating calendar feeds
DTSTART:20190306T150000Z
DTEND:20190306T160000Z
DTSTAMP:20190419T135034Z
END:VEVENT
END:VCALENDAR
我们投入大量资源来创建课堂开源软件包。您可以通过购买我们的付费产品之一来支持我们。
非常感谢您向我们发送您的家乡明信片,并提到您正在使用的包裹。您将在我们的联系页面上找到我们的地址。我们在虚拟明信片墙上发布了所有收到的明信片。
您可以通过作曲家安装软件包:
composer require spatie/icalendar-generator包装的V1和V2之间存在一些重大变化。检查升级指南以获取更多信息。
这是您可以创建日历的方式:
$ calendar = Calendar :: create ();您可以为日历命名:
$ calendar = Calendar :: create ( ' Laracon Online ' );可以将描述添加到日历中:
$ calendar = Calendar :: create ()
-> name ( ' Laracon Online ' )
-> description ( ' Experience Laracon all around the world ' );最后,您想将日历转换为文本,以便可以将其流式传输或下载给用户。这是您这样做的方式:
Calendar :: create ( ' Laracon Online ' )-> get (); // BEGIN:VCALENDAR ...将日历流式传输到应用程序时,可以在几分钟内按持续时间设置日历的刷新间隔。设置此设置时,日历应用程序将在指定的持续时间后每次检查您的服务器,以更改日历:
Calendar :: create ( ' Laracon Online ' )
-> refreshInterval ( 5 )
. . .可以创建事件如下。不需要一个名称,但应始终给出开始日期:
Event :: create ( ' Laracon Online ' )
-> startsAt ( new DateTime ( ' 6 march 2019 ' ));您可以在事件上设置以下属性:
Event :: create ()
-> name ( ' Laracon Online ' )
-> description ( ' Experience Laracon all around the world ' )
-> uniqueIdentifier ( ' A unique identifier can be set here ' )
-> createdAt ( new DateTime ( ' 6 march 2019 ' ))
-> startsAt ( new DateTime ( ' 6 march 2019 15:00 ' ))
-> endsAt ( new DateTime ( ' 6 march 2019 16:00 ' ));是否想快速创建一个开始和结束日期的事件?
Event :: create ( ' Laracon Online ' )
-> period ( new DateTime ( ' 6 march 2019 ' ), new DateTime ( ' 7 march 2019 ' ));您可以将位置添加到一个事件中:
Event :: create ()
-> address ( ' Kruikstraat 22, 2018 Antwerp, Belgium ' )
-> addressName ( ' Spatie HQ ' )
-> coordinates ( 51.2343 , 4.4287 )
. . .您可以设置活动的组织者,需要电子邮件地址,但可以省略该名称:
Event :: create ()
-> organizer ( ' [email protected] ' , ' Ruben ' )
. . .可以添加活动的与会者:
Event :: create ()
-> attendee ( ' [email protected] ' ) // only an email address is required
-> attendee ( ' [email protected] ' , ' Brent ' )
. . .您还可以设置与会者的参与状态:
Event :: create ()
-> attendee ( ' [email protected] ' , ' Ruben ' , ParticipationStatus :: accepted ())
. . .参与状态有五个:
ParticipationStatus::accepted()ParticipationStatus::declined()ParticipationStatus::tentative()ParticipationStatus::needs_action()ParticipationStatus::delegated()您可以指出参与者需要RSVP参加活动:
Event :: create ()
-> attendee ( ' [email protected] ' , ' Ruben ' , ParticipationStatus :: needs_action (), requiresResponse: true )
. . .一个事件可以使事件透明,因此它不会在日历中与其他事件以视觉重叠:
Event :: create ()
-> transparent ()
. . .可以创建一个跨越一整天的活动:
Event :: create ()
-> fullDay ()
. . .可以设置事件的状态:
Event :: create ()
-> status ( EventStatus :: cancelled ())
. . .有三个事件状态:
EventStatus::confirmed()EventStatus::cancelled()EventStatus::tentative()可以将活动分类( public , private , confidential ):
Event :: create ()
-> classification ( Classification :: private ())
. . .您可以添加URL附件:
Event :: create ()
-> attachment ( ' https://spatie.be/logo.svg ' )
-> attachment ( ' https://spatie.be/feed.xml ' , ' application/json ' )
. . .您可以添加一个嵌入式附件(base64):
Event :: create ()
-> embeddedAttachment ( $ file -> toString ())
-> embeddedAttachment ( $ fileString , ' application/json ' )
-> embeddedAttachment ( $ base64String , ' application/json ' , needsEncoding: false )
. . .您可以添加图像:
Event :: create ()
-> image ( ' https://spatie.be/logo.svg ' )
-> image ( ' https://spatie.be/logo.svg ' , ' text/svg+xml ' )
-> image ( ' https://spatie.be/logo.svg ' , ' text/svg+xml ' , Display :: badge ())
. . .有四种不同的图像显示类型:
Display::badge()Display::graphic()Display::fullsize()Display::thumbnail()创建事件后,应将其添加到日历中。有多种选择要这样做:
// As a single event parameter
$ event = Event :: create ( ' Creating calendar feeds ' );
Calendar :: create ( ' Laracon Online ' )
-> event ( $ event )
. . .
// As an array of events
Calendar :: create ( ' Laracon Online ' )
-> event ([
Event :: create ( ' Creating calender feeds ' ),
Event :: create ( ' Creating contact lists ' ),
])
. . .
// As a closure
Calendar :: create ( ' Laracon Online ' )
-> event ( function ( Event $ event ){
$ event -> name ( ' Creating calender feeds ' );
})
. . .您可以使用流行的碳库:
use Carbon Carbon ;
Event :: create ( ' Laracon Online ' )
-> startsAt ( Carbon :: now ())
. . .事件将使用您提供的DateTime对象中定义的时区。 PHP总是在DateTime对象中设置这些时区。默认情况下,这将是UTC时区,但可以更改此区域。
只是一个提醒:请勿在DateTime对象上使用PHP的setTimezone函数,它将根据时区更改时间!最好使用时区创建一个新的DateTime对象:
new DateTime ( ' 6 march 2019 15:00 ' , new DateTimeZone ( ' Europe/Brussels ' ))可以为省略时区提出一个点。例如,当您想在世界中午展示活动时。我们在12点钟定义中午,但是那个时候是相对的。对于比利时,澳大利亚或世界上任何其他国家的人们来说,情况并非如此。
这就是为什么您可以在事件上禁用时区:
$ starts = new DateTime ( ' 6 march 2019 12:00 ' )
Event :: create ()
-> startsAt ( $ starts )
-> withoutTimezone ()
. . .您甚至可以禁用整个日历的时区:
Calendar :: create ()
-> withoutTimezone ()
. . .每个日历都应具有描述日历中使用的时区组件。尽管并非所有日历客户端都需要此内容,但建议添加这些组件。
创建这样的时区组件非常复杂。这就是为什么此软件包将无需配置而自动为您添加它们。
您可以禁用此行为:
Calendar :: create ()
-> withoutAutoTimezoneComponents ()
. . .如果需要,您可以手动将时区添加到日历中:
$ timezoneEntry = TimezoneEntry :: create (
TimezoneEntryType :: daylight (),
new DateTime ( ' 23 march 2020 ' ),
' +00:00 ' ,
' +02:00 '
);
$ timezone = Timezone :: create ( ' Europe/Brussels ' )
-> entry ( $ timezoneEntry )
. . .
Calendar :: create ()
-> timezone ( $ timezone )
. . .稍后将更多地了解这些时区。
警报允许日历客户端发送有关特定事件的提醒。例如,iPhone上的Apple Mail将向用户发送有关该事件的通知。一个警报始终属于事件的描述,并且在事件触发之前几分钟。
Event :: create ( ' Laracon Online ' )
-> alertMinutesBefore ( 5 , ' Laracon online is going to start in five minutes ' );事件发生后,您还可以触发警报:
Event :: create ( ' Laracon Online ' )
-> alertMinutesAfter ( 5 , ' Laracon online has ended, see you next year! ' );或在特定日期触发警报:
Event :: create ( ' Laracon Online ' )
-> alertAt (
new DateTime ( ' 05/16/2020 12:00:00 ' ),
' Laracon online has ended, see you next year! '
);在日历或事件上删除时区也将删除警报中的时区。
活动有可能重复,例如您的每月公司晚餐。可以这样做:
Event :: create ( ' Laracon Online ' )
-> repeatOn ( new DateTime ( ' 05/16/2020 12:00:00 ' ));您还可以在一组日期上重复该事件:
Event :: create ( ' Laracon Online ' )
-> repeatOn ([ new DateTime ( ' 05/16/2020 12:00:00 ' ), new DateTime ( ' 08/13/2020 15:00:00 ' )]);简而言之,复发规则或rrule的规则可以通过描述在rrule中重复何时在日历中添加重复事件。首先,我们必须创建一个rr子:
$ rrule = RRule :: frequency ( RecurrenceFrequency :: daily ());该规则描述了将每天重复的事件。您还可以将频率设置为secondly , minutely , hourly , weekly或monthly yearly
可以将rr子添加到事件中:
Event :: create ( ' Laracon Online ' )
-> rrule ( RRule :: frequency ( RecurrenceFrequency :: monthly ()));有可能对您的个人品味进行修复;让我们看看吧!
rr子可以从某个时间点开始:
$ rrule = RRule :: frequency ( RecurrenceFrequency :: daily ())-> starting ( new DateTime ( ' now ' ));并在某个时刻停下来:
$ rrule = RRule :: frequency ( RecurrenceFrequency :: daily ())-> until ( new DateTime ( ' now ' ));它只能重复几次,例如10次:
$ rrule = RRule :: frequency ( RecurrenceFrequency :: daily ())-> times ( 10 );重复的间隔可以更改:
$ rrule = RRule :: frequency ( RecurrenceFrequency :: daily ())-> interval ( 2 );例如,当该活动在星期一开始时,该活动的下一次重复将不会在星期二,而是星期三。您可以为所有频率做同样的事情。
也可以在特定的工作日重复活动:
$ rrule = RRule :: frequency ( RecurrenceFrequency :: monthly ())-> onWeekDay (
RecurrenceDay :: friday ()
);或在当月一个星期的特定工作日:
$ rrule = RRule :: frequency ( RecurrenceFrequency :: monthly ())-> onWeekDay (
RecurrenceDay :: friday (), 3
);或一个月的最后一个工作日:
$ rrule = RRule :: frequency ( RecurrenceFrequency :: monthly ())-> onWeekDay (
RecurrenceDay :: sunday (), - 1
);您可以在本月的特定日期重复:
$ rrule = RRule :: frequency ( RecurrenceFrequency :: monthly ())-> onMonthDay ( 16 );甚至有可能在当月提供数天:
$ rrule = RRule :: frequency ( RecurrenceFrequency :: monthly ())-> onMonthDay (
[ 5 , 10 , 15 , 20 ]
);可以重复一定月份(例如,仅在第二季度):
$ rrule = RRule :: frequency ( RecurrenceFrequency :: monthly ())-> onMonth (
[ RecurrenceMonth :: april (), RecurrenceMonth :: may (), RecurrenceMonth :: june ()]
);或仅在一个月:
$ rrule = RRule :: frequency ( RecurrenceFrequency :: monthly ())-> onMonth (
RecurrenceMonth :: october ()
);可以设置一周开始的一天:
$ rrule = RRule :: frequency ( RecurrenceFrequency :: monthly ())-> weekStartsOn (
ReccurenceDay :: monday ()
);您可以提供未重复事件的特定日期:
Event :: create ( ' Laracon Online ' )
-> rrule ( RRule :: frequency ( RecurrenceFrequency :: daily ()))
-> doNotRepeatOn ( new DateTime ( ' 05/16/2020 12:00:00 ' ));还可以提供一系列不会重复事件的日期:
Event :: create ( ' Laracon Online ' )
-> rrule ( RRule :: frequency ( RecurrenceFrequency :: daily ()))
-> doNotRepeatOn ([ new DateTime ( ' 05/16/2020 12:00:00 ' ), new DateTime ( ' 08/13/2020 15:00:00 ' )]);另外,您可以将rrules添加为字符串:
Event :: create ( ' SymfonyCon ' )
-> rruleAsString ( ' FREQ=DAILY;INTERVAL=1 ' );如果将rrules添加为字符串,则DTSTART中包含的时区,直到包装不知道,因为字符串永远不会被解析和评估。如果他们知道他们可以添加DTSTART,直到单独直至帮助包装发现时区:
Event :: create ( ' SymfonyCon ' )
-> rruleAsString (
' DTSTART=20231207T090000Z;FREQ=DAILY;INTERVAL=1;UNTIL=20231208T090000Z ' ,
new DateTime ( ' 7 december 2023 09:00:00 ' , new DateTimeZone ( ' UTC ' )),
new DateTime ( ' 8 december 2023 09:00:00 ' , new DateTimeZone ( ' UTC ' ))
);您可以使用Laravel响应来流到日历应用程序:
$ calendar = Calendar :: create ( ' Laracon Online ' );
return response ( $ calendar -> get ())
-> header ( ' Content-Type ' , ' text/calendar; charset=utf-8 ' );如果要添加用户下载日历并将其导入日历应用程序的可能性:
$ calendar = Calendar :: create ( ' Laracon Online ' );
return response ( $ calendar -> get (), 200 , [
' Content-Type ' => ' text/calendar; charset=utf-8 ' ,
' Content-Disposition ' => ' attachment; filename="my-awesome-calendar.ics" ' ,
]);如果您想自己制作时区组件,那么您就在正确的位置,尽管我们建议您首先从RFC阅读时区。
您可以创建一个时区:
$ timezone = Timezone :: create ( ' Europe/Brussels ' );可以提供最后修改的日期:
$ timezone = Timezone :: create ( ' Europe/Brussels ' )
-> lastModified ( new DateTime ( ' 16 may 2020 12:00:00 ' ));或添加一个有关时区的更多信息:
$ timezone = Timezone :: create ( ' Europe/Brussels ' )
-> url ( ' https://spatie.be ' );一个时区由多个条目组成,其中时区的时间相对于UTC发生了变化,可以为标准或日光时间构建此类条目:
$ entry = TimezoneEntry :: create (
TimezoneEntryType :: standard (),
new DateTime ( ' 16 may 2020 12:00:00 ' ),
' +00:00 ' ,
' +02:00 '
);首先,您提供条目类型( standard或daylight )。然后在时间更改时进行DateTime 。最后,更改之前的相对于UTC的偏移相对于UTC的偏移,而相对于UTC的偏移偏移。
也可以为此条目提供名称和描述:
$ entry = TimezoneEntry :: create (...)
-> name ( ' Europe - Brussels ' )
-> description ( ' Belgian timezones ftw! ' );可以给出该条目的rrule:
$ entry = TimezoneEntry :: create (...)
-> rrule ( RRule :: frequency ( RecurrenceFrequency :: daily ()));最后,您可以将条目添加到时区:
$ timezone = Timezone :: create ( ' Europe/Brussels ' )
-> entry ( $ timezoneEntry );甚至添加多个条目:
$ timezone = Timezone :: create ( ' Europe/Brussels ' )
-> entry ([ $ timezoneEntryOne , $ timezoneEntryTwo ]);现在,我们已经构建了我们的时区,是时候(?)将这个时区添加到我们的日历中:
$ calendar = Calendar :: create ( ' Calendar with timezones ' )
-> timezone ( $ timezone );也可以添加多个时区:
$ calendar = Calendar :: create ( ' Calendar with timezones ' )
-> timezone ([ $ timezoneOne , $ timezoneTwo ]);我们尝试使此软件包尽可能直接。这就是为什么此软件包中未包含来自RFC的许多属性和子组件的原因。如果您可能需要包装中不包含的内容,我们已经可以在每个组件中添加其他属性或子组件。但是要小心!从这一刻起,您可以正确实施RFC。
您可以将新属性添加到这样的组件:
Calendar :: create ()
-> appendProperty (
TextProperty :: create ( ' ORGANIZER ' , ' [email protected] ' )
)
. . .在这里,我们添加了TextProperty ,这是一种默认的键值属性类型,带有文本为值。您还可以使用软件包中包含的默认属性之一,也可以通过扩展Property类来创建自己的默认属性。
有时,属性可能具有一些其他参数,这些参数是键值条目,可以将其添加到属性中:
$ property = TextProperty :: create ( ' ORGANIZER ' , ' [email protected] ' )
-> addParameter ( Parameter :: create ( ' CN ' , ' RUBEN VAN ASSCHE ' ));
Calendar :: create ()
-> appendProperty ( $ property )
. . .子组件可以这样附加:
Calendar :: create ()
-> appendSubComponent (
Event :: create ( ' Extending icalendar-generator ' )
)
. . .可以通过扩展Component类来创建子组件。
composer test我们努力易于使用API。想要更多吗?然后查看Markus Poerschke的此包。
有关最近发生了变化的更多信息,请参见ChangElog。
请有关详细信息,请参阅贡献。
如果您找到了有关安全性的错误,请邮件[email protected]而不是使用问题跟踪器。
您可以自由使用此软件包,但是如果它可以进入您的生产环境,我们非常感谢您向我们向家乡的明信片发送一张明信片,并提及您使用的哪个包装。
我们的地址是:Spatie,Kruikstraat 22,Box 12,2018 Antwerp,比利时。
我们在我们的公司网站上发布了所有收到的明信片。
麻省理工学院许可证(麻省理工学院)。请参阅许可证文件以获取更多信息。