开发者

selecting a day of the week every x number of days thats not a weekend

I have code already for开发者_StackOverflow a date range that works fairly well (not using datetime class, I'm stuck in 5.2). But I want to modify it to select the previous day before the weekend (e.g. friday) based on every fortnight from the starting date.

I have been experimenting with finding all the saturdays and sundays then doing a array_search with a mktime as such: mktime(0, 0, 0, date("m", $date), date("d", $date)-1, date("Y", $date)

It seems to find all the Saturdays & Sundays but my code seems replace the actual dates instead of just selecting the previous day and styling it.

My code is a bit of a mess, but its pretty generic date range, e.g. while ($date1 <= $date2) { do stuff here }

So how can I select the friday before a weekend of a number of every 14 days from the start date.

Edit: The answers below were all fine, but not quite what I was asking. Hopefully I can be more clear here. I don't want code that finds me all the fridays in a date range specifically, I want to do a date range and every time there is a friday that is +14 days from the start date (i.e. every 2 weeks) I want to style it so it stands out. I have managed to find all the weekends every 2 weeks and style it, but I can't figure out how to select the friday's 2 weeks in advance from the start date.


You can use strtotime() to get the initial Friday and then you can either use that or your mktime function to do your 14 day adds.


$thisWeekFriday = strtotime('friday');
$twoWeeksFromFriday = $thisWeekFriday + 1209600; //1209600 seconds in two weeks
echo date('Y-m-d H:i:s', $twoWeeksFromFriday);


$number_of_dates = 10;
for ($i = 0; $i < $number_of_dates; $i++) {
   echo date('Y-m-d', strtotime('Friday +' . ($i * 2) . ' weeks'));
}

today this would output

2011-08-12
2011-08-26
2011-09-09
2011-09-23
2011-10-07
2011-10-21
2011-11-04
2011-11-18
2011-12-02
2011-12-16
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜