在 PHP/MySQL 中创建定期日历事件
我正在尝试创建一个包含重复事件(例如每周或每月)的事件日历,但我无法理解它。有人能给我一些指点吗?这样做的最佳方法是什么?非常感谢任何帮助。谢谢。
I am trying to create an event calendar with recurring events (ex. weekly or monthly) but I cannot wrap my head around it. Can anyone give me some pointers? What is the best way to go about doing this? Any help is greatly appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建三个表,其结构如下:
event
表schedule
表schedule_recurring
表在
event
表中,schedule_type 字段将为 0 或 1,这将向应用程序指示调度信息存储在哪个表中,以及如何解释该信息。非重复事件将存储在
schedule
中,日期时间:2011-09-06 00:00:00,重复事件将存储在schedule_recurring
中,日期时间为:2011-09-06 00:00:00 :“每个第一个星期一”,时间:09:30,12:20(如果该事件在该月的每个第一个星期一发生两次)。也许这会帮助您入门!
Create three tables with a structure like:
event
tableschedule
tableschedule_recurring
tableIn your
event
table, the schedule_type field will be either 0 or 1, which would indicate to the application which table the scheduling information is stored in, as well as how to interpret that information.A non-recurring event will be stored in
schedule
with a datetime: 2011-09-06 00:00:00, and recurring events will be stored inschedule_recurring
with a date: 'every 1st Monday' and a time: 09:30,12:20 (If the event occurs twice on every first Monday of the month).Maybe this will help get you started!
我知道这是一篇旧文章,但我也在想同样的事情。
我喜欢这个人的解决方案,即使用多个表来完成此操作,但实际上这一切都可以从一张表中完成。
创建一个包含以下数据的表...
recur_mode 可以有三种状态 -
0 = 没有复发
1 = 有结束日期的重复
2 = 正在进行,没有结束日期(例如,如果您想添加诸如 1 月 1 日之类的内容作为新年),
如果日期重复出现,则 recur_code 将在其中存储 NULL 或代码。应该使用的代码是 PHP DateInterval 代码(即 P1Y 为 1 年或 P3M(3 个月)或 P7D(7 天)等) - 如果您想稍微缩小数据,您可以砍掉初始值'P' 并稍后将其添加回来,因为最初的 'P' 始终是 P,它代表周期,因此“周期 3 个月”“周期 7 天”等。
然后当您从数据库 - 您通过以下搜索检索所有数据
( end_date >= CURDATE () ) OR ( ( recur_mode = 1 ) AND ( recur_end_date >= CURDATE () ) ) OR ( recur_mode = 2 )
(请注意,这不是正确的 SQL - 这只是您需要的 or 语句的基本示例)
,然后一旦您检索到数据,就使用带有 while 循环和 DateInterval 的 PHP增加 start_date 直到下一次重新发生,同时确保如果 recur_mode 设置为 1,则开始日期不晚于 recur_end_date。
所有这些都在一个表中完成 - 而且如果您想要一个输入表单来放入代码,然后使用带有日期间隔值的隐藏字段,同时使用各种单选按钮选择间隔 - 然后使用 jQuery onchange 来更新隐藏值新的选择器值。
I know this is an old post but I was thinking the same thing too.
I like the persons solution about using multiple tables to do it, but in fact it can all be done from one table.
Create a table with the following data...
recur_mode can have three states -
0 = no recurrence
1 = recurrence with end date
2 = ongoing with no end date (e.g. if you want to add something like 1st Jan as New Years Day)
recur_code would store either NULL or a code in it if the date recurs. The code that should be used there would be the PHP DateInterval code (i.e. P1Y for 1 year or P3M (3 months) or P7D (7 days), etc) - if you want to shrink the data a bit you could chop off the initial 'P' and add it back later as the initial 'P' is always going to be P, it stands for Period so "Period 3 Months" "Period 7 Days", etc.
Then when your retrieving data from the database - you retrieve all data with the following searches
( end_date >= CURDATE () ) OR ( ( recur_mode = 1 ) AND ( recur_end_date >= CURDATE () ) ) OR ( recur_mode = 2 )
(please note this isn't proper SQL - it's just a basic example of the or statement you'd need)
then once you've retrieved the data use PHP with a while loop and DateInterval to increase the start_date until you get to the next re-occurrence also making sure that if recur_mode is set to 1 the start date is not after the recur_end_date.
All done in one table - and also if you want an input form to put the code in then use a hidden field with the dateinterval value in whilst using various radio buttons to select the interval - then use jQuery onchange to update the hidden value with the new selector values.