Note that there are some explanatory texts on larger screens.

plurals
  1. POFind weekly periods (starting on a Monday) for a month
    primarykey
    data
    text
    <p>I'm trying to find the weekly periods for a given month and year. Dates should start on a Monday and end on a Sunday. If the 1st of the month is a Sunday (Ex May 2011), it should be the first element.</p> <p>May 2011</p> <ul> <li>May 1 (Sunday)</li> <li>May 2 - May 8 (Monday - Sunday)</li> <li>May 9 - May 15 (Monday - Sunday)</li> <li>May 17 - Ma6y 22 (Monday - Sunday)</li> <li>May 23 - May 29 (Monday - Sunday)</li> <li>May 30 - May 31 (Monday - Tuesday)</li> </ul> <p>September 2012</p> <ul> <li>September 1 - September 2</li> <li>September 3 - September 9</li> <li>September 10 - September 16</li> <li>September 17 - September 23</li> <li>September 24 - September 30</li> </ul> <p>I am using this function to calculate the week numbers for two dates - I.e. the 1st day of the month and last day of the month.</p> <pre><code>public function getWeekNumbers($startDate, $endDate) { $p = new DatePeriod( new DateTime($startDate), new DateInterval('P1W'), new DateTime($endDate) ); $weekNumberList = array(); foreach ($p as $w) { $weekNumber = $w-&gt;format('W'); $weekNumberList[] = ltrim($weekNumber, '0'); } return $weekNumberList; } </code></pre> <p>Strangely, for the month of January, it returns week numbers of [52, 1, 2, 3, 4] when I'm expecting [1, 2, 3, 4, 5]. </p> <p>Once I have the week numbers, I'm using them like so:</p> <pre><code>//The following loop will populate the dataset with the entire month's durations - regardless if hours were worked or not. $firstDayOfMonth = date('Y-m-d', strtotime("first day of {$this-&gt;year}-{$monthName}")); $lastDayOfMonth = date('Y-m-d', strtotime("last day of {$this-&gt;year}-{$monthName}")); foreach ($this-&gt;getWeekNumbers($firstDayOfMonth, $lastDayOfMonth) as $key =&gt; $weekId) { // find first mоnday of the year $firstMon = strtotime("mon jan {$this-&gt;year}"); // calculate how many weeks to add $weeksOffset = $weekId - date('W', $firstMon); $beginDays = $weeksOffset * 7; $endDays = ($weeksOffset * 7) + 6; $searchedMon = strtotime(date('Y-m-d', $firstMon) . " +{$beginDays} days"); $searchedSun = strtotime(date('Y-m-d', $firstMon) . " +{$endDays} days"); echo date("M d", $searchedMon) . " - " . date("M d", $searchedSun); } </code></pre> <p>Since, the getWeekNumbers function isn't returning the week numbers I'm expecting, it's not surprising that the output of the above function is</p> <ul> <li>Dec 24 - Dec 30 (2012)</li> <li>Jan 02 - Jan 08 (2012)</li> <li>Jan 09 - Jan 15 (2012)</li> <li>Jan 16 - Jan 22 (2012)</li> <li>Jan 23 - Jan 29 (2012)</li> </ul> <p>Note that the 1st line (Dec 24 - Dec 30) is the end of the current year (2012) and not the end of last year (2011).</p> <p>Ideally, I want it to look like <img src="https://i.stack.imgur.com/5dwaW.jpg" alt="enter image description here"></p> <p>Any ideas? Thanks!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload