• 0

Split array into weeks (this week / next week) by date?


Question

I am trying to build a list of events that is split into weeks.. more specifically: this week, next week and then anything after.

 

Most of the help I can find online is relative to the current day so it's not accurate.

 

I want the 1st heading 'This week' to list all events happening this week (mon - sun) regardless of what day of the week it is... in this weeks case the week starts on 01/06/15 and ends on 07/06/15 < this is true for any date between 01-07 ..

 

The 2nd heading would be 'Next week' and would list all events happening next week 08/06/15 - 14/06/15

 

Since tomorrow is 08/06/15 all events listed here would be be moved to 'This week'.

 

In short, I need to be able to get week start (mon) and end (sun) of the current week, and the following week.

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

php:

$dayofweek = date("N"); //Return a value between 1 to 7 (Monday to Sunday)

$weekStartEpoch = strtotime("-".($dayofweek-1)." days");
$weekEndEpoch = strtotime("+".(7-$dayofweek)." days");

//You can convert above epoch times to any date format you want
echo date('r', $weekStartEpoch);
echo "</br>";
echo date('r', $weekEndEpoch);  

Haven't tested above but it should work.

 

Edit: tested and fixed some small bugs.

Edit2: fixed weekstart fail.

Link to comment
Share on other sites

  • 0

php:

$dayofweek = date("N"); //Return a value between 1 to 7 (Monday to Sunday)

$weekStartEpoch = strtotime("-".($dayofweek-1)." days");
$weekEndEpoch = strtotime("+".(7-$dayofweek)." days");

//You can convert above epoch times to any date format you want
echo date('r', $weekStartEpoch);
echo "</br>";
echo date('r', $weekEndEpoch);  

Haven't tested above but it should work.

 

Edit: tested and fixed some small bugs.

Edit2: fixed weekstart fail.

 

 

Works perfect :) thanks

Link to comment
Share on other sites

  • 0

Works perfect :) thanks

If you need help with getting the array changed into the weekly array feel free to ask ^^
Link to comment
Share on other sites

This topic is now closed to further replies.