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];
}