0

So I have the following for loop:

$arr = [];
for($x = date('j', strtotime('this week')); $x <= date('j', strtotime('this week + 6 days')); $x++)
            {
                dd('test');
                $arr[$x] = [
                    'y' => $x,
                    's' => 0,
                    'c' => 0,
                    'cl' => 0
                ];
            }
            dd('test2');

The problem is that the loop is never accessed...the dd() returns 'test2' but the for loop is not accessed and it doesn't return any errors. I have a similar one:

for($x = 1; $x <= date('t', strtotime('today')); $x++)
            {
                $arr[$x] = [
                    'y' => $x . ' ' . date('M', strtotime('today')),
                    's' => 0,
                    'c' => 0,
                    'cl' => 0
                ];
            }

And this one works perfectly. What I am trying to do is generate indexes for $arr based on this week's days or in the following case based on this month's days. I simply don't understand why one works and the other doesn't. Thank you all for your time and help!

Alphonse
  • 661
  • 1
  • 12
  • 29

3 Answers3

2

What you have with:

$x = date('j', strtotime('this week')); //returns 29

$x <= date('j', strtotime('this week + 6 days')) // returns 4

And never enter to the loop (the operation is: 29 < 4).

If you change the j for a z (in the PHP oficial documentation for date:

The day of the year (starting from 0)

) you will obtain the:

x: 148 < x: 154

Then, inside the loop, you can use the j to store the day of the week, and the loop will work.

If you want to go between 2 dates, adding days, there is others solutions too like:How to count days between two dates in PHP?

Hope it helps!

JP. Aulet
  • 4,375
  • 4
  • 26
  • 39
  • The day of the year won't work in the last week of December. – Barmar Jun 01 '17 at 10:56
  • It works, but the count starts in 0. If you want to work with dates, PHP has a very powerful options with date, strtotime and so one. You can even work with leap year or know which day of the month was 12/02/1893 (for instance). Maybe the solution here is work with pure date in the for, like (in pseudocode): x=date(now); x – JP. Aulet Jun 01 '17 at 11:01
  • In the last week of the year, it will try to loop from something like 360 to 2, and that will have the same problem as going from 29 to 4 using the day of the month. – Barmar Jun 01 '17 at 11:03
  • Thank you! Due to your explanation I figured out a way to adapt my code. – Alphonse Jun 01 '17 at 11:35
0
$date = new DateTime('this week Sunday');
$endDate = new DateTime('this week Saturday');

while( $date<=$endDate ){
   // do some stuff
   // ...

   $date->modify('+1 day');
}
George Dryser
  • 322
  • 1
  • 7
  • Can you please explain what your solution is doing instead of just pasting a piece of code – Eduardo Russo Jun 01 '17 at 17:43
  • it's very self explanatory nothing fancy here just looping though days of current week using DateTime class and while loop, the loop will stop execution after reaching Saturday and uses $date object as reference. – George Dryser Jun 02 '17 at 02:05
0

Soution found due to @JP. Aulet's explanation:

for($x = 0; $x <= 6; $x++)
            {
                $date = date('j', strtotime('this week +' .$x . 'days'));
                $arr[$date] = [
                    'y' => $date,
                    's' => 0,
                    'c' => 0,
                    'cl' => 0
                ];
            }
Alphonse
  • 661
  • 1
  • 12
  • 29