不得不承认,这真是一个奇葩的需求,无奈写个类凑活用用. 输入日期格式或者 时间戳,返回当月有多少个周一.周二.周三.....周日;
思路就是 找到这个月有多少天,在便利判断. 稍微考虑下闰年的情况
前面的获取一个月有多少天,没事看了下date函数说明,蛋疼的直接有这个参数,今天在这嘲笑一下之前的草包行为.date('t');
<?php class GAO{ public function __CONSTRUCT($time='') { if(empty($time)) $time=time(); $time = is_int($time) ? $time : strtotime($time); $this->year = date('Y',$time ); if(date('L',$time)) { $this->mouths = array( 31=> array(1,3,5,7,8,10,12), 30=> array(4,6,9,11), 29=> array(2), ); }else{ $this->mouths = array( 31=> array(1,3,5,7,8,10,12), 30=> array(4,6,9,11), 28=> array(2), ); } $this->mouth = date('m',$time ); } public function week_num() { foreach ($this->mouths as $key => $value) { if(in_array($this->mouth, $value)) { $n = $key; break; } } if(!isset($n) || empty($n)) { return '天数获取失败!'; } $result = array(1=>0,0,0,0,0,0,0); for ($n; $n >0 ; $n--) { switch (date('N',strtotime($this->year.'-'.$this->mouth.'-'.$n) ) ) { case '1': $result[1] += 1; break; case '2': $result[2] += 1; break; case '3': $result[3] += 1; break; case '4': $result[4] += 1; break; case '5': $result[5] += 1; break; case '6': $result[6] += 1; break; case '7': $result[7] += 1; break; } } return $result; } }