php返回一个日历数据:
/*获取日历*/
function getCalendar()
{
$year = !empty($_GET['year'])?intval($_GET['year']):date('Y');//当前年
$month = !empty($_GET['month'])?intval($_GET['month']):date('m');//当前月
$start_time = strtotime(date($year . '-' . $month . '-01 00:00:00'));
$end_time = strtotime(date($year . '-' . $month . '-'.date('t',$start_time).' 23:59:59'));
$day_now = date('Ymd');//当前时间
$end_days = date('w', $end_time);//当月最后一天星期几
$start_days = date('w', $start_time);//当月第一天星期几
//上月剩余天数
$last_rest = $start_days;
$calendar = [];
for ($i = $last_rest; $i > 0; $i--) {
$calendar[] = [
'date' => date('Y-m-d', $start_time - $i * 24 * 3600),
'day' => intval(date('j', $start_time - $i * 24 * 3600)),
'is_same_mouth' => date('n', $start_time - $i * 24 * 3600) == $month ? 1 : 0,//0-不是当月 1-是当月
'is_now' => (date('Ymd', $start_time - $i * 3600) == $day_now) ? 1 : 0,//2-不是当天 1-是当天
];
}
//当月第一排天数
$start = 7 - $start_days;
$time_start = $start_time;
for ($i = $start; $i > 0; $i--) {
$calendar[] = [
'date' => date('Y-m-d', $time_start),
'day' => intval(date('j', $time_start)),
'is_same_mouth' => date('n', $time_start) == $month ? 1 : 0,//0-不是当月 1-是当月
'is_now' => (date('Ymd', $time_start) == $day_now) ? 1 : 0,//0-不是当天 1-是当天
];
$time_start += 24 * 3600;
}
//中间整一个星期在当月中
$middle = ((date('t', $start_time) - (7 - $start_days) - ($end_days + 1)) / 7);
for ($j = 1; $j <= $middle; $j++) {
for ($k = 0; $k < 7; $k++) {
$calendar[] = [
'date' => date('Y-m-d', $time_start),
'day' => intval(date('j', $time_start)),
'is_same_mouth' => date('n', $time_start) == $month ? 1 : 0,//0-不是当月 1-是当月
'is_now' => (date('Ymd', $time_start) == $day_now) ? 1 : 0,//0-不是当天 1-是当天
];
$time_start += 24 * 3600;
}
}
//当月最后一排剩余天数
$last = $end_days + 1;
for ($i = 0; $i < $last; $i++) {
$calendar[] = [
'date' => date('Y-m-d', $time_start),
'day' => intval(date('j', $time_start)),
'is_same_mouth' => date('n', $time_start ) == $month ? 1 : 0,//0-不是当月 1-是当月
'is_now' => (date('Ymd', $time_start) == $day_now) ? 1 : 0,//0-不是当天 1-是当天
];
$time_start += 24 * 3600;
}
//下个月在显示的天数
$next_mouth = 7 - ($end_days + 1);
for ($i = 0; $i < $next_mouth; $i++) {
$calendar[] = [
'date' => date('Y-m-d', $time_start),
'day' => intval(date('j', $time_start)),
'is_same_mouth' => date('n', $time_start ) == $month ? 1 : 0,//0-不是当月 1-是当月
'is_now' => (date('Ymd', $time_start) == $day_now) ? 1 : 0,//0-不是当天 1-是当天
];
$time_start += 24 * 3600;
}
echo json_encode($calendar,JSON_UNESCAPED_UNICODE);
exit();
}结果返回(2021/04):
[
{
"date": "2021-03-28",
"day": 28,
"is_same_mouth": 0,
"is_now": 0
},
{
"date": "2021-03-29",
"day": 29,
"is_same_mouth": 0,
"is_now": 0
},
{
"date": "2021-03-30",
"day": 30,
"is_same_mouth": 0,
"is_now": 0
},
{
"date": "2021-03-31",
"day": 31,
"is_same_mouth": 0,
"is_now": 0
},
{
"date": "2021-04-01",
"day": 1,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-02",
"day": 2,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-03",
"day": 3,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-04",
"day": 4,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-05",
"day": 5,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-06",
"day": 6,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-07",
"day": 7,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-08",
"day": 8,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-09",
"day": 9,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-10",
"day": 10,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-11",
"day": 11,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-12",
"day": 12,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-13",
"day": 13,
"is_same_mouth": 1,
"is_now": 1
},
{
"date": "2021-04-14",
"day": 14,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-15",
"day": 15,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-16",
"day": 16,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-17",
"day": 17,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-18",
"day": 18,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-19",
"day": 19,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-20",
"day": 20,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-21",
"day": 21,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-22",
"day": 22,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-23",
"day": 23,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-24",
"day": 24,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-25",
"day": 25,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-26",
"day": 26,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-27",
"day": 27,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-28",
"day": 28,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-29",
"day": 29,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-04-30",
"day": 30,
"is_same_mouth": 1,
"is_now": 0
},
{
"date": "2021-05-01",
"day": 1,
"is_same_mouth": 0,
"is_now": 0
}
]