Pick the date range separately by weeks of the current month

Asked

Viewed 480 times

2

I need to take the day that starts and ends each week of the current month:

1 - 2019-10-01 to 2019-10-04

2 - 2019-10-07 to 2019-10-11


$today_date = date("Y-m-d");
$currentWeek = ceil((date("d",strtotime($today_date)) 
                   - date("w",strtotime($today_date)) - 1) / 7) + 1;

$i = 1;
while ($i <= $currentWeek) {
    echo "X";
    //$last_week_end = date("Y-m-d", strtotime("last week friday"));
    $i++;
}

3 answers

1

By your examples, you only want the days from Monday to Friday of the current month. So, you can take advantage of the relative formats to iterate through the dates:

$data = new DateTime('first day of this month');
$dow = $data->format('N');
if ($dow == 6 || $dow == 7) {
    // se for sábado ou domingo, ajusta para a próxima segunda-feira
    $data->modify('Monday');
}
$mes = $data->format('m');
$mesmo_mes = TRUE;
while ($mesmo_mes) {
    $inicio = $data->format("Y-m-d");
    $data->modify('Friday');
    if ($mes != $data->format('m')) {
        // foi para o mês seguinte, voltar para o mês atual e ajustar para para o último dia
        $data = new DateTime('last day of this month');
        $mesmo_mes = FALSE;
    }
    echo  "{$inicio} - {$data->format('Y-m-d')} <br>";
    $data->modify('Monday');
    if ($mes != $data->format('m')) {
        break; // se a próxima segunda-feira está no mês seguinte, sai do loop
    }
}

I start on the first day of the month and go until next Friday (using modify('Friday')). In case Friday is next month, I make an adjustment for the last day of the month.

I also treat other cases, as if the first day of the month falls on a weekend, I set for next Monday. In the middle of the loop, if the next Monday is next month (i.e., the month can end on Saturday or Sunday), I don’t need to print the respective week.

Then I print the beginning and end of each week in the format indicated (year-month-day).

1

Depending on the day, there may be some errors in your application, so it is important to check which week is getting the days (this is because some places the week starts on Monday, not Sunday)

    $sunday = new DateTime('2019-10-29');
    $sunday->modify(($sunday->format('w') === '0') ? 'sunday this week' : 'sunday last week');

    $saturday = new DateTime('2019-10-29');
    $saturday->modify(($saturday->format('w') === '6') ? 'now' : 'saturday this week');

    echo 'semana começa em:'. $sunday->format('d/m/y');
    echo '<br>';
    echo 'semana termina em:'. $saturday->format('d/m/y');

after that just add that within a foreach or some other loop with the month count

1

Use relative formats https://www.php.net/manual/en/datetime.formats.relative.php

  $dataInicial = new DateTime('first day of this month');
  $dataFinal = new DateTime('last day of this month');

  echo  'Data Inicial: '.$dataInicial->format("d/m/Y").'<br>';
  echo  'Data Final: '.$dataFinal->format("d/m/Y").'<br>';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.