简体   繁体   English

在 PHP/MySQL 中创建周期性日历事件

[英]Creating recurring calendar events in 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.谢谢。

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...使用以下数据创建一个表...

event_title
event_text
event_image
and_other_fields_about_event
recur_code (text)
recur_mode (integer)
start_date (date)
end_date (date)
recur_end_date (date)

recur_mode can have three states - 0 = no recurrence 1 = recurrence with end date 2 = ongoing with no end date (eg if you want to add something like 1st Jan as New Years Day) recur_mode 可以有三种状态 - 0 = 不重复 1 = 重复并有结束日期 2 = 正在进行但没有结束日期(例如,如果您想将 1 月 1 日之类的内容添加为元旦)

recur_code would store either NULL or a code in it if the date recurs.如果日期重复,recur_code 将在其中存储 NULL 或代码。 The code that should be used there would be the PHP DateInterval code (ie 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.应该在那里使用的代码是 PHP DateInterval 代码(即 1 年的 P1Y 或 P3M(3 个月)或 P7D(7 天)等) - 如果您想稍微缩小数据,您可以砍掉初始'P' 并稍后将其添加回来,因为最初的 'P' 始终是 P,它代表 Period 所以“Period 3 Months”“Period 7 Days”等。

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 ) ( 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) (请注意,这不是正确的 SQL - 它只是您需要的 or 语句的基本示例)

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.然后,一旦您检索到数据,请使用带有 while 循环和 DateInterval 的 PHP 来增加 start_date,直到您再次出现下一次,同时确保如果 recur_mode 设置为 1,则开始日期不在 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.全部在一张表中完成 - 如果您想要一个输入表单来放置代码,那么使用带有 dateinterval 值的隐藏字段,同时使用各种单选按钮来选择间隔 - 然后使用 jQuery onchange 更新隐藏值新的选择器值。

Create three tables with a structure like:创建三个具有如下结构的表:

event table event

-id
-schedule_type
-schedule_id
-title
etc.

schedule table schedule

-id
-event_id
-datetime

schedule_recurring table schedule_recurring

-id
-event_id
-date
-time

In 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.在您的event表中,schedule_type 字段将为 0 或 1,这将向应用程序指示调度信息存储在哪个表中,以及如何解释该信息。

A non-recurring event will be stored in schedule with a datetime: 2011-09-06 00:00:00, and recurring events will be stored in schedule_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).非重复事件将存储在schedule ,日期时间:2011-09-06 00:00:00,重复事件将存储在schedule_recurring ,日期:'every 1st Monday',时间:09:30, 12:20(如果该事件在每个月的第一个星期一发生两次)。

Maybe this will help get you started!也许这会帮助你开始!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM