tags:

views:

55

answers:

1

I'm writing a price calculator where weekends have a special price. To do this I need to know how many weekends there are between 2 dates.

Note that by a weekend, I mean if they want to have thursday to tuesday, the saturday and the Sunday are not 2 days with a weekend rate, but 1 weekend with a single rate. Thus for Thursday -> Tuesday, that would be a count of 1 weekend.

For Saturday -> Saturday that would be a count of 2 weekends, as the first saturday is not the same weekend as the following saturday. The same would also be true of Sunday to Sunday, it is counting weekends, regardless of whether they are full weekends, or partial weekends. As long as some or any portion of the weekend falls within that date range, it counts as 1 weekend. The best analogy I can think of is to consider the weekend a single day that lasts 48 hours, not 2 that last 24.

So, how would I determine how many 'whole or partial weekends' there are between 2 dates in php?

+1  A: 

We have to start with determining the number of days between the dates. That would be something like

$n = floor((timestamp_end - timestamp_start)/86400);

If you say that Saturday->Saturday = 2, then unless our start day is a Saturday and the amount of days is a product of 7, the number of weekends will be $n/7, otherwise, $n/7+1. So we could start off with.

$n_weekends = ceil($n/7);

And then

if (date("N", timestamp_start) == 6 && $n%7 == 0 ){
  $n_weekends++;  
}
Vlad
does this account for it the start day is a sunday? remember sunday -> sunday the following week also counts as 2 weekends
Tom J Nowell