網上有很多漂亮的js日歷插件拿來做一下日期的選擇方便、漂亮,但是如果要拿來做像訂票、行程表等,這些都需要將我們的數據動態的寫入日歷中,這種就需要大量的改動,所以我們用php加js生成一個能夠高度定制的日歷.
生成日歷的代碼
<!--?php
namespaceOrg\Util;
classCalendar{
private$year;
private$month;
private$day;
private$weeks=array('日','一','二','三','四','五','六');
function__construct($options=array()){
$this-year=$options['year'];
$this-month=$options['month'];
$this-day=$options['day'];
$vars=get_class_vars(get_class($this));
foreach($optionsas$key=$value){
if(array_key_exists($key,$vars)){
$this-$key=$value;
}
}
}
functiondisplay()
{
$list='
';
$list.=$this-showChangeDate();
$list.=$this-showWeeks();
$list.=$this-showDays($this-year,$this-month,$this-day);
return$list;
}
privatefunctionshowWeeks()//生成星期
{
$list='';
foreach($this-weeksas$title)
{
$list.='';
}
$list.='';
return$list;
}
privatefunctionshowDays($year,$month,$day)
{//生成日期,具體日歷的樣式可以替換生成
$firstDay=mktime(0,0,0,$month,1,$year);
$starDay=date('w',$firstDay);
$days=date('t',$firstDay);
$list='';
for($i=0;$i$starDay;$i++){
$list.='';
}
for($j=1;$j=$days;$j++){
$i++;
$ymd=date('Y-m-d',strtotime($year.'-'.$month.'-'.$j));
$time=strtotime($year.'-'.$month.'-'.$j);
if($j==$day){//當前日期下
$list.='';
}else{//非當前日期下
$list.='';
}
if($i%7==0){//一個星期結束
$list.='';
}
}
$list.='
| '.$title.' | ||
|---|---|---|
| '.$j.' | '.$j.' | '.$j.' |
return$list;
}
functionshowChangeDate()//點擊更換月份{
$url=basename($_SERVER['PHP_SELF']);
$list='
';
return$list;
}
privatefunctionpreYearUrl($year,$month)
{
$year=($this-year=1970)?1970:$year-1;
return'year='.$year.'month='.$month;
}
privatefunctionnextYearUrl($year,$month)
{
$year=($year=2038)?2038:$year+1;
return'year='.$year.'month='.$month;
}
privatefunctionpreMonthUrl($year,$month)
{
if($month==1){
$month=12;
$year=($year=1970)?1970:$year-1;
}else{
$month--;
}
return'.$year.','.$month.';//'year='.$year.'month='.$month;
}
privatefunctionnextMonthUrl($year,$month)
{
if($month==12){
$month=1;
$year=($year=2038)?2038:$year+1;
}else{
$month++;
}
return'.$year.','.$month.';//'year='.$year.'month='.$month;
}
}調用日歷
publicfunctionget_date(){
if(IS_AJAX){
$result['title']='success';
$params=array();
$year=I('get.year',date('Y'));
$month=I('get.month',date('m'));
$day=I('get.day',date('d'));
if($year$month){
$params=array(
'year'=$year,
'month'=$month,
'day'=$day,
);
}
$catobj=new\Org\Util\Calendar($params);
//print_r($price_list);
$result['content']=$catobj-display();
$this-ajaxReturn($result);
}
}
前端js生成日歷
chage_month(year,month,day);//當前的年月日
functionchage_month(year,month,day){
$.ajax({url:{:urlrotue('Activity/get_date')}?year=+year+month=+month+day=+day,
type:POST,
cache:false,
data:{},
success:function(obj){
if(obj.title=='success'){
$('#get_date').html(obj.content);
}
else{
ayer.msg(obj.msg,{icon:5});}
},
error:function(){
ayer.msg('獲取日期失敗!',{icon:5});}
});
}
效果:
這個樣式比較集成,只加了跳轉時間篩選功能,我們可以在日期showDays里面添加日期的活動,門票等,看業務需求.