tags:

views:

194

answers:

2

What is an easy way to get every Friday, Saturday and Sunday betwee two dates, without using the datetime object of PHP5.0.

Because of the speed I don't want to traverse every date between 1/1 and 31/12. (per page I have to calculate it at least 50 times :) as part of a result view.)

+2  A: 

Good news is that you don't have to traverse every date as Friday always comes five days after Sunday!

Anyway, there's roughly 18000 days between 2000AD and 2050AD, which means you could build yourself a Date -> DayOfWeek mapping within a few minutes, which you can reuse for the next few years to come.

Zed
+2  A: 
$start = strtotime("today"); // your start/end dates here
$end = strtotime("today + 1 years");

$friday = strtotime("friday", $start);
while($friday <= $end) {
    echo "fri=", date("d m Y", $friday), "\n";
    echo "sat=", date("d m Y", strtotime("+1 days", $friday)), "\n";    
    echo "sun=", date("d m Y", strtotime("+2 days", $friday)), "\n\n";    
    $friday = strtotime("+1 weeks", $friday);
}

surely can be optimized, but you get the idea

stereofrog
I'm a big fan of Strtotime
ChronoFish