fullCalendar动态获取数据


fullCalendar http://fullcalendar.io/docs/event_data/events_function

$('#calendar').fullCalendar({
	header: {
        left: 'prev,next',
        center: 'title',
        right: 'today'
    },
    defaultDate: '2016-08-20',
    lang: 'zh-cn',
    buttonIcons: false, // show the prev/next text
    weekMode: 'liquid',
    events: function(start,end,timezone, callback) {
    	var date = this.getDate().format('YYYY-MM');
        $.ajax({
            url: '?m=home&c=product&a=ajax&todo=ajaxGetProductMonthPrice',
            dataType: 'json',
            data: {
                id:{$id},
                date: date,
            },
            success: function(json) { // 获取当前月的数据
                var events = [];
                if (json.status == '1') {
                    $.each(json.info,function(i,c) {
                        if (c.is_special == '1') {
                            events.push({
                                title: '¥'+c.price+','+c.stock+'套',
                                start: c.date , // will be parsed
                                color: '#FFEBAC'
                            });
                        } else {
                            events.push({
                                title: '¥'+c.price+','+c.stock+'套',
                                start: c.date , // will be parsed
                                color: '#BEEABE'
                            });
                        }
                        
                    });
                }
                callback(events);
            }
        });
    }
});

var date = this.getDate().format('YYYY-MM');
每次点击上一月,下一月都会获取月份。
把月份传入后台,获取数据。

$id  = trim($_REQUEST['id']);
$date= trim($_REQUEST['date']);
$r = $this->product_db->get_one(array('id'=>$id));
if (empty($r)) { // 不存在
    $rdata['status'] = 2;
    $rdata['msg']    = '房源不存在';
} else {
    $rdata['status'] = 1;
    $rdata['msg']    = '获取成功';
    // 获取目标月份对应的数据
    $special_type = $r['special_type']; // 0 无特价 1 周五,周六 2 周六,周日 3 周五,周六,周日
    $special_price= $r['special_price'];
    $price        = $r['price'];
    $stock        = $r['num'];
    $days = get_day($date,'2');
    foreach ($days as $k => $day) {
    	$week = get_week($day);
    	$info[$k]['date'] = $day;
    	switch ($special_type) {
    		case '0':
    			$info[$k]['price'] = $price;
    			$info[$k]['is_special']= '0';
    			break;
    		case '1':
    			if (in_array($week,array('星期五','星期六'))) {
    				$info[$k]['price'] = $special_price;
    				$info[$k]['is_special']= '1';
    			} else {
    				$info[$k]['price'] = $price;
    				$info[$k]['is_special']= '0';
    			}
    			break;
    		case '2':
    			if (in_array($week,array('星期六','星期日'))) {
    				$info[$k]['price'] = $special_price;
    				$info[$k]['is_special']= '1';
    			} else {
    				$info[$k]['price'] = $price;
    				$info[$k]['is_special']= '0';
    			}
    			break;
    		case '3':
    			if (in_array($week,array('星期五','星期六','星期日'))) {
    				$info[$k]['price'] = $special_price;
    				$info[$k]['is_special']= '1';
    			} else {
    				$info[$k]['price'] = $price;
    				$info[$k]['is_special']= '0';
    			}
    			break;
    		default:
    			
    			break;
    	}
    	$info[$k]['isRent'] = '1'; // 1表示可租
    	$info[$k]['stock']  = $stock;
    }

    $rdata['info'] = $info;
}

exit(json_encode($rdata));
break;

get_day方法,获取当月的每天日期

/**
 * 获取当月天数
 * add by Jim
 * @param  $date 
 * @param  $rtype  1天数 2具体日期数组
 * @return 
 */
function get_day( $date ,$rtype = '1')   
{
    $tem = explode('-' , $date);       //切割日期  得到年份和月份
    $year = $tem['0'];
    $month = $tem['1'];
    if( in_array($month , array( 1 , 3 , 5 , 7 , 8 , 01 , 03 , 05 , 07 , 08 , 10 , 12)))
    {
        // $text = $year.'年的'.$month.'月有31天';
        $text = '31';
    }
    elseif( $month == 2 )
    {
        if ( $year%400 == 0  || ($year%4 == 0 && $year%100 !== 0) )        //判断是否是闰年
        {
            // $text = $year.'年的'.$month.'月有29天';
            $text = '29';
        }
        else{
            // $text = $year.'年的'.$month.'月有28天';
            $text = '28';
        }
    }
    else{

        // $text = $year.'年的'.$month.'月有30天';
        $text = '30';
    }

    if ($rtype == '2') {
        for ($i = 1; $i <= $text ; $i ++ ) {
        	if ($i < 10) {
        		$i = '0'.$i;
        	}
            $r[] = $year."-".$month."-".$i;
        }
    } else {
        $r = $text;
    }
    return $r;
}

get_week获取具体日期对应的星期几,根据后台数据,设置返回信息。周末特价。

/**
 * 获取星期几
 * add by Jim
 */
function get_week($date){
    //强制转换日期格式
    $date_str=date('Y-m-d',strtotime($date));

    //封装成数组
    $arr=explode("-", $date_str);
     
    //参数赋值
    //年
    $year=$arr[0];
     
    //月,输出2位整型,不够2位右对齐
    $month=sprintf('%02d',$arr[1]);
     
    //日,输出2位整型,不够2位右对齐
    $day=sprintf('%02d',$arr[2]);
     
    //时分秒默认赋值为0;
    $hour = $minute = $second = 0;   
     
    //转换成时间戳
    $strap = mktime($hour,$minute,$second,$month,$day,$year);
     
    //获取数字型星期几
    $number_wk=date("w",$strap);
     
    //自定义星期数组
    // $weekArr=array("7","1","2","3","4","5","6");
    $weekArr=array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
     
    //获取数字对应的星期
    return $weekArr[$number_wk];
}
原文地址:https://www.cnblogs.com/jiqing9006/p/5795799.html