PHP开发之制作简单日历生成日历的各个边界值

113.png

自定义函数threshold方法,生成日历的各个边界值

1)计算这个月总天数

2)计算这个月第一天与最后一天,各是星期几

3)计算日历中的第一个日期与最后一个日期

<?php
function threshold($year, $month) {
    $firstDay = mktime(0, 0, 0, $month, 1, $year);
    $lastDay = strtotime('+1 month -1 day', $firstDay);
    //取得天数  
    $days = date("t", $firstDay);
    //取得第一天是星期几
    $firstDayOfWeek = date("N", $firstDay);
    //获得最后一天是星期几
    $lastDayOfWeek = date('N', $lastDay);
    //上一个月最后一天
    $lastMonthDate = strtotime('-1 day', $firstDay);
    $lastMonthOfLastDay = date('d', $lastMonthDate);
    //下一个月第一天
    $nextMonthDate = strtotime('+1 day', $lastDay);
    $nextMonthOfFirstDay = strtotime('+1 day', $lastDay);
    
    //日历的第一个日期
    if($firstDayOfWeek == 7){
      $firstDate = $firstDay;
    }else{
      $firstDate = strtotime('-' . $firstDayOfWeek . ' day', $firstDay);
    }
    //日历的最后一个日期
    if($lastDayOfWeek == 6){
      $lastDate = $lastDay;
    }elseif($lastDayOfWeek == 7){
      $lastDate = strtotime('+6 day', $lastDay);
    }else{
      $lastDate = strtotime('+' . (6 - $lastDayOfWeek) . ' day', $lastDay);
    }
    
    return array(
    'days' => $days, 
    'firstDayOfWeek' => $firstDayOfWeek, 
    'lastDayOfWeek' => $lastDayOfWeek,
    'lastMonthOfLastDay' => $lastMonthOfLastDay,
    'firstDate' => $firstDate,
    'lastDate' => $lastDate,
    'year' => $year,
    'month' => $month
    );
}
?>

注释:

mktime() 函数返回日期的 UNIX 时间戳。

strtotime() 函数将任何英文文本的日期或时间描述解析为 Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数)。

Weiter lernen
||
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
function threshold($year, $month) {
$firstDay = mktime(0, 0, 0, $month, 1, $year);
$lastDay = strtotime('+1 month -1 day', $firstDay);
//
$days = date("t", $firstDay);
//
$firstDayOfWeek = date("N", $firstDay);
//
$lastDayOfWeek = date('N', $lastDay);
//
$lastMonthDate = strtotime('-1 day', $firstDay);
$lastMonthOfLastDay = date('d', $lastMonthDate);
//
$nextMonthDate = strtotime('+1 day', $lastDay);
$nextMonthOfFirstDay = strtotime('+1 day', $lastDay);
//
if($firstDayOfWeek == 7){
$firstDate = $firstDay;
}else{
$firstDate = strtotime('-' . $firstDayOfWeek . ' day', $firstDay);
}
//
if($lastDayOfWeek == 6){
$lastDate = $lastDay;
}elseif($lastDayOfWeek == 7){
$lastDate = strtotime('+6 day', $lastDay);
}else{
$lastDate = strtotime('+' . (6 - $lastDayOfWeek) . ' day', $lastDay);
}
return array(
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
einreichenCode zurücksetzen
图片放大关闭