2013-04-14 68 views
0
特定的一天

事件發生我使用jQuery FullCalendar http://arshaw.com/fullcalendar/docs/event_data/events_json_feed/jQuery的FullCalendar就

試圖建立每週發生的事件。

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

是否有無論如何我可以將它翻譯成Fullcalendar事件對象?

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

這是爲只讀顯示? – charlietfl

+0

是隻讀顯示 – user2242631

+0

您使用什麼服務器語言?如果您計劃將插件事件映射到服務器,可能最容易做到這一點url – charlietfl

回答

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 
    } 
] 
+0

謝謝你給我一個很好的起點。 – user2242631