【发布时间】:2013-03-23 03:22:06
【问题描述】:
更新: 下面是我原来的问题。查看我的答案,看看我是如何解决的。
我正在尝试使用 MySQL 数据库表“churchcal_events”中的事件填充我的日历。我可能有针对特定日期的一次性活动和可以设置为每周一、每隔一个周四或每月第二个周五的重复活动。
一次性事件没问题。每周的活动都有效(每周,每隔一周)。但是每个月的事件仅在第一个月显示,而不是在接下来的几个月显示。
churchcal_events 表 - 排除对该问题不重要的字段
+----+----------------+------------+-----------+------------+------------+
| id | name | recurring | frequency | recur_type | recur_day |
+----+----------------+------------+-----------+------------+------------+
| 1 | Test Weekly | 1 | 1 | W | Sunday |
| 2 | Test Bi-Weekly | 1 | 2 | W | Monday |
| 3 | Test Monthly | 1 | 1 | M | Friday |
+----+----------------+------------+-----------+------------+------------+
php 代码 - 每月每一天的循环内
//query all events
$get_events = db_query("SELECT * FROM {churchcal_events}
WHERE (MONTH(date) = :month AND YEAR(date) = :year AND DAY(date) = :day) OR
(recurring = :recur AND recur_day LIKE :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) {
//if recurring, see if event should be on this day
if($event->recurring == '1') {
$recur_day = $event->recur_day;
$recur_freq = $event->frequency;
$recur_type = $event->recur_type;
$recur_start = new DateTime(date('Y-m-d', strtotime($event->recur_start)));
$recur_end = new DateTime(date('Y-m-d', strtotime($event->recur_end)));
$recur_start->modify($recur_day);
$recur_interval = new DateInterval("P{$recur_freq}{$recur_type}");
$recur_period = new DatePeriod($recur_start, $recur_interval, $recur_end);
foreach($recur_period as $recur_date) {
if($recur_date->format('Ymd') == date('Ymd', strtotime($month . '/' . $list_day . '/' . $year))) {
$calendar .= calendar_event($event-id, $event->name, $event->starttime);
}
}
}
如何使示例 Churchcal_events 表中的 ID“3”显示在每个月的第一个星期五?
【问题讨论】:
-
你的 db_query() 函数是什么样的?
-
@cpattersonv1 这是Drupal 7 db_query() 函数。
-
您是否检查了变量(如 recur_period)的值?同样来自 PHP doc: m/d/y 或 d-m-y 格式的日期通过查看各个组件之间的分隔符来消除歧义:如果分隔符是斜杠 (/),则假定为美式 m/d/y;而如果分隔符是破折号 (-) 或点 (.),则假定为欧洲 d-m-y 格式。为避免潜在的歧义,最好尽可能使用 ISO 8601 (YYYY-MM-DD) 日期或 DateTime::createFromFormat()。
-
另外,我会验证运行 PHP 的服务器上的日期/时间。有时,当服务器时间与您认为的不同时,您会得到意想不到的结果......
-
@Revent 日期是这样正确的。