简体   繁体   中英

Jquery FullCalendar Make event occur on specific day

I'm using Jquery FullCalendar http://arshaw.com/fullcalendar/docs/event_data/events_json_feed/

Trying to set up events that occur every week.

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

Is there anyway I can translate that into Fullcalendar Event objects ?

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

This php code should get you going:

/* 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 );
};

Sample Output:

[
    {
        "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
    }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM