更新: 以下は私の元の質問です。私の回答を参照して、どのように解決したかを確認してください。
MySQL データベース テーブル 'churchcal_events' のイベントをカレンダーに入力しようとしています。特定の日付の 1 回限りのイベントと、毎週月曜日、隔週木曜日、または毎月第 2 金曜日に設定できる定期的なイベントがある場合があります。
1回限りのイベントは問題ありません。また、毎週のイベントが機能します (毎週、隔週)。ただし、毎月のイベントは最初の月にのみ表示され、次の月には表示されません。
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' を毎月第 1 金曜日に表示するにはどうすればよいですか?