9

In case anyone would like to do something similar, I will post the code that I wrote and tested within the last five hours.

如果有人想做類似的事情,我會發布我在過去五個小時內編寫和測試的代碼。

I decided to convert my two tables into three, moving all of the recurring options to the third table:

我決定將我的兩個表轉換為三個,將所有重復選項移動到第三個表:

'churchcal_events' - there are additional fields for event info that I haven't included here

'churchcal_events' - 我還沒有包含此事件信息的其他字段

+----+-----------+------------+------------+------------+-----------+

| id | name | date | starttime | endtime | recurring |

+----+-----------+------------+------------+------------+-----------+

| 1 | Event 1 | 2013-04-01 | 10:30:00 | 12:00:00 | 0 |

| 2 | Event 2 | | 14:00:00 | 15:00:00 | 1 |

| 3 | Event 3 | | 09:00:00 | | 1 |

| 4 | Event 4 | | 19:00:00 | 21:00:00 | 1 |

+----+-----------+------------+------------+------------+-----------+

'churchcal_assign' - routes events to appropriate calendars (because there will be calendars for different departments throughout the website)

'churchcal_assign' - 將事件路由到適當的日歷(因為整個網站的不同部門都會有日歷)

+----------+-------------+

| event_id | calendar_id |

+----------+-------------+

| 1 | 1 |

| 2 | 1 |

| 3 | 1 |

| 4 | 1 |

+----------+-------------+

'churchcal_recur_events' - Here are the rules for each event's recurrence

'churchcal_recur_events' - 以下是每個事件重復發生的規則

+----------+-----------+------------+----------------+------------+-------------+-----------+

| event_id | frequency | recur_type | recur_day_num | recur_day | recur_start | recur_end |

+----------+-----------+------------+----------------+------------+-------------+-----------+

| 2 | 1 | Week | NULL | Sunday | NULL | NULL |

| 3 | 2 | Week | NULL | Wednesday | 2013-04-01 | NULL |

| 4 | 2 | Month | third | Friday | 2013-04-01 | NULL |

+----------+-----------+------------+----------------+------------+-------------+-----------+

New Code (PHP) - This is in a day loop

新代碼(PHP) - 這是一天的循環

//query all events

$get_events = db_query("SELECT * FROM {churchcal_events ce, churchcal_recur_events cre}

WHERE (MONTH(ce.date) = :month AND YEAR(ce.date) = :year AND DAY(ce.date) = :day) OR

(ce.id = cre.event_id AND ce.recurring = :recur AND cre.recur_day = :calendar_day)

ORDER BY starttime",

array(

':month' => $month,

':year' => $year,

':day' => $list_day,

':recur' => '1',

':calendar_day' => date('l', strtotime($month . '/' . $list_day . '/' . $year)),

));

foreach($get_events as $event) {

//see if events belong to this calendar

$calendar_assign = db_query("SELECT * FROM {churchcal_assign} WHERE event_id = :event_id AND calendar_id = :cal_id",

array(

':event_id' => $event->id,

':cal_id' => $cal_id,

));

if($calendar_assign->rowCount() > 0) {

//one-time events

if(($event->recurring != '1') && ($single_event_id != $event->id)) {

$calendar .= calendar_event($event->id, $event->name, $event->starttime);

$single_event_id = $event->id;

//$calendar .= $single_event_id;

}

//monthly recurring events

if(($event->recur_type == 'Month') && ($event->recurring == '1')

//day is on or before end date

&& (($event->recur_end >= date('Y-m-d', strtotime($month . '/' . $list_day . '/' . $year))) || ($event->recur_end == '0000-00-00') || ($event->recur_end == NULL))

//day is on or after start date

&& (($event->recur_start <= date('Y-m-d', strtotime($month . '/' . $list_day . '/' . $year))) || ($event->recur_start == '0000-00-00') || ($event->recur_start == NULL))

//day is equal to date of recurrence, i.e. first friday, third monday, etc

&& ($year . date('m', strtotime($year . '/' . $month . '/' . $list_day)) . $list_day == date('Ymj', strtotime($event->recur_day_num . ' ' . $event->recur_day . ' of ' . date('F', strtotime($month . '/' . $list_day . '/' . $year)) . ' ' . $year)))

//day is in agreement with month frequency, i.e. every month, every other, etc. from start date

&& ($month % $event->frequency == date('m', strtotime($event->recur_start)) % $event->frequency)) {

$calendar .= calendar_event($event->id, $event->name, $event->starttime);

}

//weekly recurring events

if(($event->recur_type == 'Week') && ($event->recurring == '1')

//day is on or before end date

&& (($event->recur_end >= date('Y-m-d', strtotime($month . '/' . $list_day . '/' . $year))) || ($event->recur_end == '0000-00-00') || ($event->recur_end == NULL))

//day is on or after start date

&& (($event->recur_start <= date('Y-m-d', strtotime($month . '/' . $list_day . '/' . $year))) || ($event->recur_start == '0000-00-00') || ($event->recur_start == NULL))

//day is in agreement with week frequency, i.e. every week, every other, etc. from start date

&& (date('W', strtotime($month . '/' . $list_day . '/' . $year)) % $event->frequency == date('W', strtotime($event->recur_start)) % $event->frequency)) {

$calendar .= calendar_event($event->id, $event->name, $event->starttime);

}

}

}

If you are not creating this in a Drupal module like I am, you can replace 'db_query()' with your own database query function like PHP's default 'mysql_query()';

如果您不是像我一樣在Drupal模塊中創建它,您可以將'db_query()'替換為您自己的數據庫查詢函數,如PHP的默認'mysql_query()';

php 写 mysql 事件_PHP日歷,包含來自MySQL數據庫的重復事件相关推荐

  1. solr 4.8 mysql_solr 4.8+mysql數據庫數據導入 + mmseg4j中文全文索引 配置筆記

    1.如何將solr部署,請參考之前的文章 2.按上述配置好后,在solr_home文件夾中,將包含collection1文件夾,這就是solr的一個實例.下面我們來看看collection1中的文件內 ...

  2. jpa oracle mysql,JPA-Oracle-Mysql連接數據庫配置

    //...省略datasouce class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" ...

  3. 數據庫ORACLE轉MYSQL存儲過程遇到的坑~(總結)

    ORACLE數據庫轉MySQL數據庫遇到的坑 總結 最近在做Oracle轉mysql的工程,遇到的坑是真的多,尤其是存儲過程,以前都沒接觸過類似的知識,最近也差不多轉完了就總結一下.希望能幫到一些人( ...

  4. php 将表情存入数据库,php + mysql 存入表情 【如何轉義emoji表情,讓它可以存入utf8的數據庫】...

    方法1:base_encode64 這種方法是可以,但是舊數據沒有經過encode操作,取數據的時候如果統一進行decode的話,舊數據會丟失的. 1 方法2:urlencode 這個似乎可以,對沒有 ...

  5. mysql數據庫的增刪改查_MySQL數據庫之基礎增刪改查操作

    作者:        守望幸福 最后修改日期:2014-7-27 所操作的MySQL數據庫名:firstdb 所用的兩個表結構如下: student表 number name socre born_d ...

  6. mysql使用从c3p0_使用c3p0連接池操作mysql數據庫中文亂碼解決辦法

    今天在一台新的CentOS機器上使用c3p0連接池操作mysql數據庫出現中文亂碼問題,具體表現為:查詢時無中文亂碼問題,寫數據時中文亂碼,查看了機器上數據庫字符集也是UTF8,應該不會出現中文亂碼才 ...

  7. mysql不能存字母,使用不常見的字母/符號時,MySql數據庫不能正確存儲數據

    I have a simple form that sends (via php) some variables to a mySql database. 我有一個簡單的表單,通過PHP發送一些變量到 ...

  8. python3.5 连接mysql_python3.5 連接mysql本地數據庫

    前期准備工作: 安裝python的模塊,網上大部分讓安裝mysqldb模塊,但是會報錯,原因是python3.5不被其支持: 請看該鏈接 我們也可以這樣解決: 直接執行:sudo pip3 insta ...

  9. mdf ldf 转mysql_如何將sql server 的.mdf和.ldf 數據備份文件導入到mysql數據庫中?

    示例 下面的示例將 pubs 中的兩個文件附加到當前服務器. EXEC sp_attach_db @dbname = N'pubs', @filename1 = N'c:\Program Files\ ...

最新文章

  1. GPU高效通信算法-Ring Allreduce
  2. oracle 日志的容量,oracle 11g 更改日志组大小
  3. 从技术角度聊聊,短视频为何让人停不下来?
  4. 通过预训练提升语言理解
  5. 信息学奥赛一本通 1312:【例3.4】昆虫繁殖
  6. [流体力学] 推导柱坐标系下的连续性方程
  7. 同为开发:有了分布式/高并发等这些Java项目经历,面大厂稳了!
  8. 简单python脚本实例-Python简单实现阴阳师挂机脚本
  9. qqwry.dat java 乱码_UTF-8使用纯真IP数据库乱码问题
  10. class文件的加载过程
  11. Office ❀ PPT图形中的合并形状梳理
  12. 山东罕见姓氏百家姓都没有,翻家谱竟是皇室后裔,专家:是真的
  13. linux安装识别不到scsi硬盘,Linux下不重起识别SCSI硬盘
  14. 读博太孤独?一个人的苦行!
  15. 关于固态硬盘的数据擦除
  16. IDEA中Git暂存代码暂存区的使用
  17. html有哪些好玩的地方,武汉本地好玩的地方有哪些?盘点武汉十大必看景点
  18. 流媒体服务器,red5,EsayDSS,AdobeMediaServer5和阿里推流服务性能对比。flash播放器JWplayer CKplayers Aliplayer性能对比
  19. CRTD--有关于intel芯片组和BCM4360网卡适配银河麒麟V10系统(适用于macbook)
  20. 最受欢迎的五大bug管理平台

热门文章

  1. java内部类的使用
  2. Linux - 简单设置 vim (tab, 行号, 换行)
  3. 耿建超英语语法---被动语态
  4. 2434: [Noi2011]阿狸的打字机
  5. java 将Map拷贝到另一个Map对象当中
  6. String,StringBuffer,StringBuilder
  7. [转]TCP协议中的三次握手和四次挥手(图解)
  8. ubuntu下查询SSH状态和安装SSH服务
  9. 导致散户亏损的三大根源
  10. 浅谈 举家搬迁静态文件到CDN