月球-I型,月份日历生成器----基于PHP7.3
- 生成月份周日的类
<?php class mycalendar { function __construct($year,$mon) { $this->nianyue=$year.'-'.$mon.'-'.'01'; $this->firstday=strtotime(date($this->nianyue)); $this->m = intval(date('m',$this->firstday)); $this->wdaylist=array(); $this->lday = strtotime(date($this->nianyue).'+1 month -1 day'); //echo "时间戳:".date('Y-m-d',$this->lday)."\n"; } function startday() { $w = date('w',$this->firstday); $diffdays=0; if ($w!=0) { $diffdays= 7-$w; } $this->firstday = strtotime(date('Y-m-d',strtotime($this->nianyue))."+".$diffdays." day"); //array_push($this->wdaylist,$w); //echo "\n".date('Y-m-d',$this->firstday); //echo "\n".$this->firstday; return $this->firstday; } function addwday() { $curday = $this->startday(); while (true){ if(intval(date('m',$curday)!=$this->m)) { break; } //echo date('Y-m-d',$curday)."\n"; array_push($this->wdaylist,date('Y-m-d',$curday)); $curday=strtotime(date('Y-m-d',$curday)."+7 day"); } return $this->wdaylist; } }
- 根据每个周日推算每个周的日期
<?php require('mycal.php'); class gencal { function __construct(array $wkday) { $this->time = strtotime($wkday[count($wkday)-1]); $this->wdaylist=$wkday; $this->ldday= date('m',$this->time); $this->ttday= date('t',$this->time); $this->firstday=strtotime($wkday[0]); } function diffdays() { $lastday = 0; if($this->ldday<$this->ttday) { $lastday = strtotime(date('Y-m-d',$this->time)."+1 day"); } //echo "this day:".date('Y-m-d',$lastday); return $lastday; } function adddays() { //生成日期辗转相加 $genarray = array(); $startday = $this->diffdays(); //echo $startday."\n"; $newday = date('Y-m-d',strtotime(date('Y-m-d',strtotime($this->wdaylist[count($this->wdaylist)-1]))."+1 day")); //echo 'newday:'. $newday; $genarray[] = $newday; for ($i=1;$i<7;$i++) { $startday=strtotime(date('Y-m-d',$startday)."+1 day"); $genarray[] = date('Y-m-d',$startday); /*array_push($genarray,strtotime(date('Y-m-d',$startday).'+1 day'));*/ } return $genarray; } function minus($startday){ //辗转相减法 $thisweek=array(); $startday = strtotime($startday); $thisweek[7]=date('Y-m-d',$startday); //echo $startday; for($i=6;$i>0;$i--) { $startday=strtotime(date('Y-m-d',$startday)."-1 day"); $thisweek[$i]=date('Y-m-d',$startday); //echo date('Y-m-d',$startday)."---"."\n"; } return $thisweek; } function gendaylist() { $finallist=array(); for ($i=0;$i<count($this->wdaylist);$i++) { array_push($finallist,$this->minus($this->wdaylist[$i])); } array_push($finallist,$this->adddays()); return $finallist; } }
- 测试
$cal = new mycalendar(2019,9); $wuwa= new gencal($cal->addwday()); //$wuwa->adddays(); print_r($wuwa->gendaylist());
- 输出结果
Array ( [0] => Array ( [7] => 2019-09-01 [6] => 2019-08-31 [5] => 2019-08-30 [4] => 2019-08-29 [3] => 2019-08-28 [2] => 2019-08-27 [1] => 2019-08-26 ) [1] => Array ( [7] => 2019-09-08 [6] => 2019-09-07 [5] => 2019-09-06 [4] => 2019-09-05 [3] => 2019-09-04 [2] => 2019-09-03 [1] => 2019-09-02 ) [2] => Array ( [7] => 2019-09-15 [6] => 2019-09-14 [5] => 2019-09-13 [4] => 2019-09-12 [3] => 2019-09-11 [2] => 2019-09-10 [1] => 2019-09-09 ) [3] => Array ( [7] => 2019-09-22 [6] => 2019-09-21 [5] => 2019-09-20 [4] => 2019-09-19 [3] => 2019-09-18 [2] => 2019-09-17 [1] => 2019-09-16 ) [4] => Array ( [7] => 2019-09-29 [6] => 2019-09-28 [5] => 2019-09-27 [4] => 2019-09-26 [3] => 2019-09-25 [2] => 2019-09-24 [1] => 2019-09-23 ) [5] => Array ( [0] => 2019-09-30 [1] => 2019-10-01 [2] => 2019-10-02 [3] => 2019-10-03 [4] => 2019-10-04 [5] => 2019-10-05 [6] => 2019-10-06 ) )