这个 php 代码应该可以帮助您:
/* create this array from SQL query*/
$events=array(
array('title'=>'Event One', 'day'=>2, 'startTime'=> '10:00:00', 'date'=>getFirstOfMonthDate(2)),
array('title'=>'Event Two', 'day'=>4, 'startTime'=> '13:00:00' ,'date'=>getFirstOfMonthDate(4))
);
/* number of weeks to generate*/
$num_weeks=10;
/* calling this function outputs recurring events JSON*/
createRecurringEventsJSON( $events, $num_weeks);
/* functions*/
function createRecurringEventsJSON( $events, $num_weeks){
$output=array();
for($i=0;$i<$num_weeks;$i++){
for($j=0; $j< count($events) ;$j++){
$output[]= createEvent($events[$j]['date'],$events[$j]['title'],$events[$j]['startTime']);
$events[$j]['date']=datePlusOneWeek($events[$j]['date']);
}
}
echo json_encode($output);
}
function datePlusOneWeek($date){
return date('Y-m-d',strtotime ( '+1 week' , strtotime ( $date ) ));
}
function getFirstOfMonthDate( $dayNumber){
/* first is blank so index 1 =Monday, 5=Friday*/
$days=array('','Monday','Tuesday','Wednesday','Thursday','Friday');
$fullMonthText=date('F');
$dayText=$days[$dayNumber];
$year = date('Y');
/* strtotime accepts "first Monday of December 2013" format*/
$textString='first '.$dayText.' of '.$fullMonthText.' '.$year;
return date('Y-m-d', strtotime($textString));
}
/* returns calendar event object*/
function createEvent($date,$title,$time){
return array('title'=>$title, 'start'=>$date.' '.$time,'allDay'=> false );
};
样本输出:
[
{
"title": "Event One",
"start": "2013-04-02 10:00:00",
"allDay": false
},
{
"title": "Event Two",
"start": "2013-04-04 13:00:00",
"allDay": false
},
{
"title": "Event One",
"start": "2013-04-09 10:00:00",
"allDay": false
},
{
"title": "Event Two",
"start": "2013-04-11 13:00:00",
"allDay": false
}
]