List current week days with PHP

Asked

Viewed 728 times

0

I have to make a calendar with PHP and is almost ready, the problem is just that for example, today (28/07/2016) the week in the calendar begins at 01/08/2016 and the right one should be 25/07/2016 until 29/07/2016 (Monday to Friday).

I have the function below:

<?php
public function DataSemana($semana = 1){

        $c = time() + 60*60*24*5*$semana;
        $date = date('Y-m-d', $c);
        $dayofweek = date('w', strtotime($date));
        $result    = strtotime((0-$dayofweek+1).' day', strtotime($date));

        $primeira_data = date('Y-m-d', $result);
        $segunda_data = date('Y-m-d', $result+(60*60*24*5));

        $inicio = new DateTime($primeira_data);
        $fim = new DateTime($segunda_data);

        //sequencia aritmetica com razao igual a 1 dia
        $intervalo = new DateInterval('P1D');

        $datas = new DatePeriod($inicio, $intervalo, $fim);

        return $datas;
    }
?>
  • 1

    Are you taking into consideration that Sunday’s response on date('w') is 6? The first day of the week, pro PHP, is Monday.

  • How could I start the week I am ? Monday to Friday ? @Williamaparecidobrandino

  • First, trying to understand why you already set the Monday parameter in the receipt of your function. He always picks up next Monday. In my view, the logic should be to search for the day of the week of the current date, and use the functions to search for the corresponding Monday.

No answers

Browser other questions tagged

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