如何处理无限重复的日期
我正在使用 PHP 和 MySQL 在网站上实现一个相当简单的日历。我希望能够处理无限重复的日期,但不确定最好的方法。
对于时间有限的重复事件,将时间范围内的每个事件添加到我的数据库表中并将它们与某种形式的递归 ID 分组似乎是有意义的。
但是,当事件重复的频率没有限制时,是否最好
a) 将特定时间范围(例如接下来的 2 年)的记录放入数据库中,然后随着时间的推移定期检查并添加新记录 -问题是,如果有人向前看 3 年,该事件将不会显示
b) 实际上并没有每个事件的记录,而是当我在 php 代码中检查指定时间段内的事件时,计算是否重复事件将在这段时间内发生 - 问题在于,这意味着每个事件都没有特定的记录,当我想将其他信息(出勤等)与该事件关联时,我会发现这是一个痛苦。看起来也可能有点慢
有人尝试过这两种方法吗?如果是这样,结果如何?或者我还缺少其他一些巧妙的方法吗?
I am implementing a fairly simple calendar on a website using PHP and MySQL. I want to be able to handle dates that repeat indefinitely and am not sure of the best way to do it.
For a time limited repeating event it seems to make sense to just add each event within the timeframe into my db table and group them with some form of recursion id.
But when there is no limit to how often the event repeats, is it better to
a) put records in the db for a specific time frame (eg the next 2 years) and then periodically check and add new records as time goes by - The problem with this is that if someone is looking 3 years ahead, the event won't show up
b) not actually have records for each event but instead when i check in my php code for events within a specified time period, calculate wether a repeated event will occur within this time period - The problem with this is that it means there isn't a specific record for each event which i can see being a pain when i then want to associate other info (attendance etc) with that event. It also seems like it might be a bit slow
Has anyone tried either of these methods? If so how did it work out? Or is there some other ingenious crafty method i'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会采取方法 b,如果有人添加一些内容,我会创建一个“真实”事件条目。
编辑:
您期望有多少个周期性事件以及那会是哪种周期性事件? (例如:每周一、每两周等)
I'd take approach b and if someone adds something to it, I'd create a "real" event entry.
Edit:
How many periodic events do you expect and what kind of periodic events would that be? (eg: every monday, every two weeks etc.)
我会为重复事件创建一条记录。然后,如果必须在特定日期添加更多信息,我将为附件创建一条记录,并引用重复事件。
I would create a single record for a repeated event. Then in case more info has to be added to a specific date, I would create a record for the attachment with a reference to the repeated event.
第三次投票支持选项 B - 理由是数据只能在有限的时间范围内(即开始和结束)进行查询。出于性能原因,我建议除了存储第一次出现的日期/时间、出现次数和频率之外,还应在数据库中维护最后一次出现的情况。
C.
Third vote for option B - rationale being that the data should only ever be queried for a limited timeframe (i.e. start and end). For performance reasons I'd suggest that, in addition to storing the date/time of the first occurrence, number of occurrences and frequency that you also maintain the last occurrence in the database.
C.
根据我的经验,生成重复日期并检查特定日期是否符合该模式在性能方面并不是那么糟糕。一年只有365天。一万天,已经快三十年了。这意味着,在实际场景中,输入/输出的大小相对较小。
这个库可能会有所帮助(但它是javascript): http://github.com/mooman/recurring_dates
From my experience, generating recurring dates and checking if a specific date is in that pattern isn't all that bad performance-wise. There's only 365 days in a year. 10,000 days is already almost 30 years. which means, the size of the input/output is relatively small in a practical scenario.
This library may help (but it's javascript): http://github.com/mooman/recurring_dates