javascript - PHP如何判断每个月的每一周是几号到几号?
PHP中文网
PHP中文网 2017-04-11 12:31:45
0
5
267

业务需要,根据当前时间判断这个月每一周是几号到几号,比如这个月,第一周是1号到5号,第二周是6号到12号。。返回数组最好,不要年的,要月,俺是菜鸟,希望大神给予代码个思路...谢谢了

PHP中文网
PHP中文网

认证0级讲师

reply all(5)
迷茫

偶来凑个热闹,来个面向对象版本的:


/**
 * @param DateTime|int|string|null $date 可以是DateTime对象、时间戳、字符串形式的日期或者空值表示当前月份
 * @return array [[1,2,3,4,5], ...] 分别表示这月每一周都是几号到几号
 */
function getWeeksOfMonth($date=null){
    if (is_numeric($date)){
        $d = new DateTime();
        $d->setTimestamp($date);
        $date = $d;
    } else if (is_string($date)){
        $date = new DateTime($date);
    } else if ($date instanceof DateTime){
        // nothing to do
    } else if (!$date){
        $date = new DateTime();
    } else {
        throw new InvalidArgumentException("Invalid type of date!");
    }


    // 当前日期是在一个月里面是第几天
    //  j: 月份中的第几天,没有前导零    1 到 31
    $dateDay = (int)$date->format('j');

    // 这个月1号是星期几
    // N: 1(表示星期一)到 7(表示星期天)
    $beginWeekDay = (int)$date->sub(new DateInterval("P" . ($dateDay - 1) . "D"))->format('N');

    // 这个月最后一天是几号
    // j    月份中的第几天,没有前导零    1 到 31
    $endMonthDay = (int)($date->add(new DateInterval('P1M'))->sub(new DateInterval("P1D"))->format('j'));

    $weeks = [];
    $indexOfWeek = 0;
    $weekDay = $beginWeekDay;

    for ($day = 1; $day <= $endMonthDay; $day++){
        if (!isset($weeks[$indexOfWeek])){
            $weeks[$indexOfWeek] = [];
        }

        $weeks[$indexOfWeek][] = $day;

        $weekDay++;
        if ($weekDay > 7){
            $weekDay = $weekDay - 7;
            $indexOfWeek++;
        }
    }

    // 只要一个星期里面的第一天和最后一天?
    foreach ($weeks as &$week) {
        $week = [$week[0], end($week)];
    }

    return $weeks;
}

// 测试一下:
foreach (getWeeksOfMonth() as $week){
    echo implode(", ", $week) . "\n";
}

// 输出:(今天是2017-03-08)
// 1, 5
// 6, 12
// 13, 19
// 20, 26
// 27, 31
// 


用到的日期时间API比较少,应该比较高效吧

大家讲道理
print_r(date('Y-m-d', strtotime('first mon of january'))); 

参考:http://uk3.php.net/manual/zh/...

for($i=1;$i<=52;$i++){
    $month = ($i < 10) ? '0'.$i : $i;
    echo '第'.$i.'周开始:';
    $monday = date('Y-m-d', strtotime('2017W'.$month));
    echo $monday;
    echo '第'.$i.'周结束:';
    $sunday = date('Y-m-d' ,strtotime($monday . '+6day'));
    echo $sunday;
    echo '<br>';
}

输出:

第1周开始:2017-01-02第1周结束:2017-01-08
第2周开始:2017-01-09第2周结束:2017-01-15

第3周开始:2017-01-16第3周结束:2017-01-22
第4周开始:2017-01-23第4周结束:2017-01-29
第5周开始:2017-01-30第5周结束:2017-02-05
第6周开始:2017-02-06第6周结束:2017-02-12
第7周开始:2017-02-13第7周结束:2017-02-19
第8周开始:2017-02-20第8周结束:2017-02-26
第9周开始:2017-02-27第9周结束:2017-03-05

再次修改:

$months = [
    'january','february','march','april','may','june','july','august ','september','october','november','december'
];
$weeks = [
    '1'=>'first','2'=>'second','3'=>'third','4'=>'fourth'
];
foreach($months as $key=>$value){
    echo $value.'<br>';
    for($i=1;$i<=4;$i++){
        echo '第'.$i.'周:';
        $monday = date('Y-m-d', strtotime($weeks[$i].' monday of '.$value));
        $sunday = date('Y-m-d' ,strtotime($monday . '+6day'));
        echo 'from '.$monday .' to '.$sunday;
        echo '<br>';
    }
}

january
第1周:from 2017-01-02 to 2017-01-08
第2周:from 2017-01-09 to 2017-01-15
第3周:from 2017-01-16 to 2017-01-22
第4周:from 2017-01-23 to 2017-01-29
february
第1周:from 2017-02-06 to 2017-02-12
第2周:from 2017-02-13 to 2017-02-19
第3周:from 2017-02-20 to 2017-02-26
第4周:from 2017-02-27 to 2017-03-05

左手右手慢动作
//需要的数组注释掉了
<?php
class GetWeeksOfMonths
{
    public $_month;

    public $_week =array( '1'=>'first','2'=>'second','3'=>'third','4'=>'fourth','5'=>'fifth');

    public $_months = array('january','february','march','april','may','june','july','august ','september','october','november','december');
    public function __construct($month)
    {
        header('Content-Type:text/html;charset:utf-8');
    
        $this->_month = $this->_months[$month];

    }
    //return array
    public function get_weeks_of_months()
    {
        $week = $this->get_total_weeks();
        //$total = array();
        for($i=1;$i<=$week;$i++)
        {
            if($i==$week)
            {
                $start ='第 '.$i.' 周: ';
                $monday =  date('Y-m-d', strtotime( $this->_week[$i].' monday of '.$this->_month));
                $sunday =  date('Y-m-d', strtotime('last day of '.$this->_month));
            }
            else
            {
                $start ='第 '.$i.' 周: ';
                $monday =  date('Y-m-d', strtotime( $this->_week[$i].' monday of '.$this->_month));
                $sunday =  date('Y-m-d', strtotime( $monday.' +6 day'));
            }
            
            $total[]=$monday.' to '.$sunday;
            
            echo $start.$monday.' to '.$sunday.'<br/>';
            
        }
        //print_r($total);
        
        
    } 
    public function get_total_weeks()
    {
        $first_monday = date('Y-m-d', strtotime('first monday of '.$this->_month));
        $last_monday = date('Y-m-d', strtotime('last monday of '.$this->_month));
        $total_weeks =(strtotime($last_monday)-strtotime($first_monday))/(7*24*60*60)+1;
        return $total_weeks;
    }
}


$a = new GetWeeksOfMonths(1);

$a->get_weeks_of_months();

?>
``

![图片描述][1]

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!