php 日历代码

时间:2024-11-24 22:04:07

日历的PHP接口代码:

$user_id = $_SESSION['user_id'];
$year = isset($_REQUEST['tty']) ? intval($_REQUEST['tty']) : date('Y');
$month = isset($_REQUEST['ttm']) ? intval($_REQUEST['ttm']) : date('m');
//获取当前月有多少天
$days = date('t', strtotime("{$year}-{$month}-1"));
// 当前1号是星期几
$week = date('w', strtotime("{$year}-{$month}-1"));
// 实现上一月和上一年
if ($month == 1) {
$premonth = 12;
$preyear = $year - 1;
} else {
$premonth = $month - 1;
$preyear = $year;
}
// 实现下一月和下一年
if ($month == 12) {
$nextmonth = 1;
$nextyear = $year + 1;
} else {
$nextmonth = $month + 1;
$nextyear = $year;
}
$dayss = array();
for ($i = 1; $i <= $week; $i++) {
$dayss[] = '';
}
for ($i = 1; $i <= $days; $i++) {
$dayss[] = $i;
}
// 获取签到所有数据(本月中)
$start = strtotime("{$year}-{$month}-1");
$end = strtotime("{$nextyear}-{$nextmonth}-1") - 1;
$logsql = 'SELECT * FROM ' . $ecs->table('hpyer_sign_log') . " where uid={$user_id} AND add_time between {$start} AND {$end}";
$loglist = $db->getAll($logsql);
$loginfo = array();
foreach ($loglist as $v) {
$loginfo[] = intval(date('d', $v['add_time']));
} $result = array(
'code' => 1,
'data' => array(
'year' => $year,
'month' => $month,
'preyear' => $preyear,
'premonth' => $premonth,
'nextyear' => $nextyear,
'nextmonth' => $nextmonth,
'days' => $dayss,
'loginfo' => $loginfo
)
);
die($jsonr->encode($result));

HTML接口说明:

$('.riQh').on('click', function() {
var tty = $(this).attr('tty'),
ttm = $(this).attr('ttm');
$.ajax({
url: '/mobile/ajaxnew.php',
data: {
act: 'rili',
tty: tty,
ttm: ttm
},
type: 'post',
dataType: 'json',
success: function(res) {
console.log(res);
if (res.code == 1) {
$('.riz').attr('tty', res.data.preyear);
$('.riz').attr('ttm', res.data.premonth);
$('.riy').attr('tty', res.data.nextyear);
$('.riy').attr('ttm', res.data.nextmonth);
$('#nowdate').text(res.data.year + '年' + res.data.month + '月');
var zoninfo = '';
for (var i = 0; i < res.data.days.length; i++) {
if(res.data.loginfo.indexOf(res.data.days[i]) >-1){
zoninfo += '<li class="active">'+res.data.days[i]+'</li>';
}else{
zoninfo += '<li>'+res.data.days[i]+'</li>';
}
}
$('#rilihtml').html(zoninfo);
} else {
alert('获取数据失败!');
}
}
});
});