0

我正在使用 Jquery FullCalendar http://arshaw.com/fullcalendar/docs/event_data/events_json_feed/

尝试设置每周发生的事件。

In my database its stored as
DayOfTheWeek 1 (1-5 specifies monday to firday)
StartTime 10:00:00
EndTime 13:00:00

无论如何我可以将其转换为 Fullcalendar Event 对象吗?

events: [{
        title: 'Event One',
        id: 1,  
        start: Monday 10AM,
        end: Monday 1PM
    },
    {
        title: 'Event Two',
        id: 2, 
        start: Tuesday 10AM,
        end: Tuesday 5pm
    },
    ]
4

1 回答 1

0

这个 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
    }
]
于 2013-04-15T01:41:35.603 回答