【问题标题】:find specific weeks in month using php使用 php 查找月份中的特定星期
【发布时间】:2014-03-11 14:17:02
【问题描述】:

我想从同月的星期一开始到星期天结束的月份中获取星期。 我会提供月份和年份。 例如:

$month = '03';
$year = '2014';

然后函数应该返回 2014-03-03 to 2014-03-09 作为月份的开始日期,因为该月的第一个星期一是 3 号。 然后继续一个月直到上周。 2014 年 3 月,第 31 日从星期一开始,但不会在 3 月结束,它会在 2014 年 6 月 4 日结束,因此不应计入。

现在,当我将月份传递为“04”时,该月份的第一周应计为 3 月 31 日。

我希望我说清楚,对不起语言。 到目前为止我已经尝试过:

$month = "04";
$year = "2014";
$beg = (int)date('W', strtotime("first day of $year-$month"));
$end = (int)date('W', strtotime("last day of $year-$month"));
$weeks = range($beg, $end);

foreach($weeks as $week) {
    $result = $this->getStartAndEndDate($week, $year);
    print_r($result);
}

function getStartAndEndDate($week, $year)
{
    $time = strtotime("1 January $year", time());
    $day = date('w', $time);
    $time += ((7*$week)+1-$day)*24*3600;
    $return[0] = date('Y-m-d', $time);
    $time += 6*24*3600;
    $return[1] = date('Y-m-d', $time);
    return $return;
}

输出:

Array
(
    [0] => 2014-03-03
    [1] => 2014-03-09
)
Array
(
    [0] => 2014-03-10
    [1] => 2014-03-16
)
Array
(
    [0] => 2014-03-17
    [1] => 2014-03-23
)
Array
(
    [0] => 2014-03-24
    [1] => 2014-03-30
)
Array
(
    [0] => 2014-03-31
    [1] => 2014-04-06
)
Array
(
    [0] => 2014-04-07
    [1] => 2014-04-13
)

【问题讨论】:

  • 为什么它在一种情况下应该来自 31.03。在另一个不是从 24.02 开始。 (一周于 02.03 结束。) - 那么你究竟是如何解决“重叠月份”的?另外,您当前的代码在哪里让您失望?
  • @kingkero 没错,它不仅仅是一个案例,它只是一个例子......
  • 在上面的输出中,我不想要最后两个数组。也 2014-03-31 应计入 2014 年 4 月(04)

标签: php date


【解决方案1】:

您可以为此使用DateTime 类:

$dt = new DateTime('1st march');

// is this a monday?
if($dt->format('N') !== '1') {
  // if not, went to next monday
  $dt->modify('next monday');
}

echo $dt->format('Y-m-d')
    . ' to ' . $dt->modify('next sunday')->format('Y-m-d');

输出:

2014-03-03 to 2014-03-09

【讨论】:

    【解决方案2】:
    <?php
    function get_weeks($month, $year) {
        $weeks = array();
    
        $date = DateTime::createFromFormat('mY', $month.$year);
        $date->modify('first Monday of this month');
    
        $end = clone $date;
        $end->modify('last Monday of this month');
        $interval = DateInterval::createFromDateString('1 week');
        $period = new DatePeriod($date, $interval, $end);
    
        $counter = 1;
        foreach ($period as $dt) {
            $end_of_week = clone $dt;
            $end_of_week->modify('next Sunday');
            $weeks[] = sprintf("Week %u: %s - %s", 
                $counter,
                $dt->format('Y-m-d'),
                $end_of_week->format('Y-m-d')
            );      
            $counter++;
        }
    
        return $weeks;
    }
    $weeks = get_weeks('03', '2014');
    print_r($weeks);
    
    Array
    (
        [0] => Week 1: 2014-03-03 - 2014-03-09
        [1] => Week 2: 2014-03-10 - 2014-03-16
        [2] => Week 3: 2014-03-17 - 2014-03-23
        [3] => Week 4: 2014-03-24 - 2014-03-30
    )
    

    See it in action

    【讨论】:

      猜你喜欢
      • 2017-08-28
      • 2013-02-15
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-17
      • 1970-01-01
      • 2014-06-26
      相关资源
      最近更新 更多