PHP只使用php获取当月的周数

时间:2017-09-15 12:11:49

标签: php arrays datetime

这是我的周数组。当前月份 9 和年份 2017 ,我尝试了以下代码,

Array
(
    [0] => Array
        (
            [ssdate] => 2017-09-01
            [eedate] => 2017-09-03
        )

    [1] => Array
        (
            [ssdate] => 2017-09-04
            [eedate] => 2017-09-10
        )

    [2] => Array
        (
            [ssdate] => 2017-09-11
            [eedate] => 2017-09-17
        )

    [3] => Array
        (
            [ssdate] => 2017-09-18
            [eedate] => 2017-09-24
        )

    [4] => Array
        (
            [ssdate] => 2017-09-25
            [eedate] => 2017-09-30
        )

    [5] => Array
        (
            [ssdate] => 2017-10-02
            [eedate] => 2017-09-30
        )

)

我从please check here这个链接获得了代码。但没有任何办法。

这里是我的周数组:

 Array
(
    [0] => Array
        (
            [sdate] => 2017-09-01
            [sdate] => 2017-09-02
            [sdate] => 2017-09-03
        )

    [1] => Array
        (
            [sdate] => 2017-09-04
            [sdate] => 2017-09-05
            [sdate] => 2017-09-06
            [sdate] => 2017-09-07
            [sdate] => 2017-09-08
            [sdate] => 2017-09-09
            [sdate] => 2017-09-10
        )
     [2] => Array
        (
            [sdate] => 2017-09-11
            [sdate] => 2017-09-12
            [sdate] => 2017-09-13
            [sdate] => 2017-09-14
            [sdate] => 2017-09-15
            [sdate] => 2017-09-16
            [sdate] => 2017-09-17
            [sdate] => 2017-09-18
        )

    [3] => Array
        (
            [sdate] => 2017-09-18
            [sdate] => 2017-09-19
            [sdate] => 2017-09-20
            [sdate] => 2017-09-21
            [sdate] => 2017-09-22
            [sdate] => 2017-09-23
            [sdate] => 2017-09-24
        )

    [4] => Array
        (
            [sdate] => 2017-09-25
            [sdate] => 2017-09-26
            [sdate] => 2017-09-27
            [sdate] => 2017-09-28
            [sdate] => 2017-09-29
            [sdate] => 2017-09-30
        )
)

但我只想要当前的月份数组,如下所示

Runtime.getRuntime().exec("su");

请帮我解决这个问题

提前致谢!!!

3 个答案:

答案 0 :(得分:1)

如果您想要当前月份数据,那么为什么要比较$end_date1(距离最后一天+7天)。您需要在for循环中添加$end_date的条件。如下所示更改循环:

for ($date = $start_date; $date < $end_date; $date = date('Y-m-d', strtotime($date . ' + 7 days'))) {
    $getarray = getWeekDates($date, $start_date, $end_date);
    $week_array[] = $getarray;
    $count_week++;
}

编辑根据您的评论,31个月后会出现问题。所以我改变了一些代码。试试吧。如下所示更改function getWeekDates

function getWeekDates($date, $start_date, $end_date) {
    $week = date('W', strtotime($date));
    $year = date('Y', strtotime($date));
    $from = date("Y-m-d", strtotime("{$year}-W{$week}+1"));

    if ($from < $start_date)
        $from = $start_date;

    $to = date("Y-m-d", strtotime("{$year}-W{$week}-7"));
    if ($to > $end_date)
        $to = $end_date;
    if($from > $end_date)
    {
      $array1 = array(
    );
    }
    else
    {
      $array1 = array(
        "ssdate" => $from,
        "eedate" => $to,
    );
    }


    return $array1;
}

然后按如下方式更改循环:

for ($date = $start_date; $date <= $end_date1; $date = date('Y-m-d', strtotime($date . ' + 7 days'))) {    
    $getarray = getWeekDates($date, $start_date, $end_date);
    if(count($getarray))
    {
      $week_array[] = $getarray;
      $count_week++;  
    }


}

答案 1 :(得分:1)

<?php

function getFullWeeksOfMonth($iYear, $iMonth, $sFirstDayOfWeek = 'Sunday', $bExclusive = true)
{
    $iYear           = filter_var($iYear, FILTER_VALIDATE_INT, array(
        'options' => array(
            'default' => (int) date('Y')
        )
    ));
    $iMonth          = filter_var($iMonth, FILTER_VALIDATE_REGEXP, array(
        'options' => array(
            'default' => (int) date('m'),
            'regexp' => '/^([1-9]|1[012])$/'
        )
    ));
    $aDay            = array(
        'monday' => 1,
        'sunday' => 1
    );
    $sFirstDayOfWeek = filter_var($sFirstDayOfWeek, FILTER_VALIDATE_REGEXP, array(
        'options' => array(
            'default' => 'sunday', //Set default week
            'regexp' => '/^monday|sunday$/'
        )
    ));
    $bExclusive      = filter_var($bExclusive, FILTER_VALIDATE_BOOLEAN);
    $oStart          = new DateTime($iYear . '-' . $iMonth . '-01');

    if ($bExclusive === true || ($bExclusive === false && isset($aDay[strtolower($oStart->format('l'))]))) {
        if ((int) $oStart->format('d') === 1) {
            $oStart->modify('-1 day');
        }
        $oStart->modify('first ' . $sFirstDayOfWeek . ' ' . $oStart->format('H:i'));
    } else {
        $oStart->modify('last ' . $sFirstDayOfWeek . ' ' . $oStart->format('H:i'));
    }

    $oEnd = clone ($oStart);
    if ((int) $oStart->format('m') === $iMonth) {
        $oEnd->modify('last day of this month');
    } else {
        $oEnd->modify('last day of next month');
    }

    $oInterval  = new DateInterval('P1W7D');
    $oDaterange = new DatePeriod($oStart, $oInterval, $oEnd);

    $aDate = array();
    $i     = 1;
    foreach ($oDaterange as $oDate) {
        $oTestDate    = clone $oDate;
        $oLastWeekDay = $oTestDate->modify('+6 days');
        if (((int) $oDate->format('m') === (int) $iMonth || (int) $oLastWeekDay->format('m') === (int) $iMonth) && (($bExclusive === true && (int) $oLastWeekDay->format('m') === (int) $iMonth) || ($bExclusive === false))) {
            $aDate[$i]['First'] = $oDate->format('Y-m-d');
            $aDate[$i]['Last']  = $oLastWeekDay->format('Y-m-d');
        }
        $i++;
    }
    return $aDate;
}

echo "<pre>";

print_r(getFullWeeksOfMonth(2017, 9, 'Sunday'));

echo "<pre>";
?>
  

$ iYear是年份的变量$ iMonth是数字   当月的代表(1 - 12)$ sFirstDayOfWeek采取任何一种方式   星期一或星期日是一周的第一天;默认是星期一。   $ bExclusive是一个布尔值,当true时只需几周   选择了一个月,但是当错误时,它允许重叠进入   帐户与所选月份相关的所有周数。

结果:

enter image description here

答案 2 :(得分:0)

function weeks_in_month($year, $month, $start_day_of_week)
  {
    // Total number of days in the given month.
    $num_of_days = date("t", mktime(0,0,0,$month,1,$year));

    // Count the number of times it hits $start_day_of_week.
    $num_of_weeks = 0;
    for($i=1; $i<=$num_of_days; $i++)
    {
      $day_of_week = date('w', mktime(0,0,0,$month,$i,$year));
      if($day_of_week==$start_day_of_week)
        $num_of_weeks++;
    }

    return $num_of_weeks;
  }
  $year = 2017;
  $i=9;
  echo $year."-".$i."=>&nbsp;".weeks_in_month($year, $i, 1).' weeks<br />';