0

Here, is my week array. The current month 9 and year 2017, I tried below code,

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;

    $array1 = array(
        "ssdate" => $from,
        "eedate" => $to,
    );

    return $array1;
}

$mm = date('m');
$yy = date('y');
$startdate = date($yy . "-" . $mm . "-01");
$current_date = date('Y-m-t');
$ld = cal_days_in_month(CAL_GREGORIAN, $mm, $yy);
$lastday = $yy . '-' . $mm . '-' . $ld;
$start_date = date('Y-m-d', strtotime($startdate));
$end_date = date('Y-m-d', strtotime($lastday));
$end_date1 = date('Y-m-d', strtotime($lastday . " + 7 days"));
$count_week = 0;
$week_array = array();

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

I got code from PHP get number of week for month this link. But not getting any way.

here, is my week array :

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
        )

)

but I want only current month array, like below

 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
        )
)

Please help me to solve this problem

Thanks in advance !!!

Puja
  • 451
  • 2
  • 5
  • 20

3 Answers3

1

if you want current month data then why are you comparing $end_date1 (which is +7 days from last day). You need to add condition for $end_date in for loop. change for loop as below :

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++;
}

EDIT as per your comment there is issue when 31 days month. So I have change some code. try it. change function getWeekDates as below:

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;
}

Then change loop as below:

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++;  
    }


}
B. Desai
  • 16,414
  • 5
  • 26
  • 47
  • your solution is correct bt if month have 31 days and for eg.october month have 31 days and last week of that month day is 29 then 30 and 31 day it will remove from the array, bt I want that. – Puja Sep 15 '17 at 12:34
  • You need to ask new question for new issue. @Angel – B. Desai Sep 16 '17 at 06:36
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 is the variable for the year $iMonth is the numerical representation of the month (1 – 12) $sFirstDayOfWeek takes either Monday or Sunday as the first day of the week; default is Monday. $bExclusive is a boolean which when true takes only weeks within the chosen month but when false, it allows overlapping to take into account all weeks that are related to the chosen month.

Results :

enter image description here

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
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 />';
karthikeyan
  • 172
  • 1
  • 9