日历编写代码,花了一点儿时间看了看网上代码
简单搞定侧边栏日历小功能,主要还是为了局部刷新,相比较还是用纯jq/js写比较好
load引入php文件或者其他文件 毕竟多出一些资源

php+jquery编写

//获取年月总天数以及一号星期几
//然后使用js的函数$('Element').load()加载php文件
//(或者加载其他文件也可以),文件内输出或返回日历table即可
//header('Content-type:text/html;charset=utf-8');
//ini_set('date.timezone','Asia/Shanghai');
$nowY  = date('Y');
$nowM  = date('m');
$nowD  = date("d");
$year  = isset($_REQUEST['year'])?$_REQUEST['year']:$nowY;
$month = isset($_REQUEST['month'])?$_REQUEST['month']:$nowM;
$north1= mktime(0,0,0,$m,1,$y);
$week  = date("w",$north1);
$days  = date("t",$north1);
$eNum  = $week==0 ? 6 : $week-1;
$str = "<table><caption>$year 年$month 月</caption><tr>";
foreach (array('周一','周二','周三','周四','周五','周六','周日')
 as $weekDay)    $str .= '<td>'.$weekDay.'</td>';
$str .= '</tr>';
for($k=1;$k <= $days;$k++){
    $current = $year==$nowY&&$month==$nowM&&$k==$nowD?'currentd':'';
    if($k == 1 && $week != 1) $str .= str_repeat('<td></td>',$eNum);
    $str .=  "<td class='$current'>$k</td>";
    if(($eNum+$k)%7==0) $str.= "</tr><tr>";
}
echo $str;

javascript/jquery编写

//获取年月总天数以及此月份一号星期几
//然后使用html()输出日历table格式即可
function getLunar(year,month){
    d        = new Date();
    nowYear  = d.getFullYear();
    nowMonth = d.getMonth()+1;
    nowDay   = d.getDate();
    if(!year||!month){
        year = nowYear;
        month = nowMonth;
    }
    week   = getMonthFirstDayWeek(year,month);
    eNum   = week===0 ? 6 : week-1;
    days   = getMonthDays(year,month);
    str    = "<table><caption>"+year+"年"+month+"月</caption>"+
        '<tr><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td><td>日</td></tr>';
    for(var k=1;k <= days;k++){
        current = year===nowYear&&month===nowMonth&&k===nowDay?'currentd':'';
        if(k === 1 && week !== 1) str += '<td></td>'.repeat(eNum);
        str +=  "<td class='"+current+"'>"+ k +"</td>";
        if((eNum+k)%7===0) str+= "</tr><tr>";
    }
    str    += '</table>';
    return str;
}
function getMonthDays(year,month){
    var dd     = new Date(year, month, 0);
    return dd.getDate();
}
function getMonthFirstDayWeek(year,month){
    var dw  = new Date(year,month-1,1);
    return dw.getDay();
}

标签: PHP, 前端

WeChatPay

微信打赏

Alipay

支付宝打赏

如果对你也有帮助,随便来个五毛的吧,一切随缘看心情...

空空如也