Skip to content

Instantly share code, notes, and snippets.

@lplume
Created October 3, 2017 08:22
Show Gist options
  • Save lplume/a58077bc8e56b40e8448ce9bf7ed487c to your computer and use it in GitHub Desktop.
Save lplume/a58077bc8e56b40e8448ce9bf7ed487c to your computer and use it in GitHub Desktop.
<?php
define('ONE_WEEK', 604800); // 7 * 24 * 60 * 60
function number_of_days($days, $start, $end) {
$w = array(date('w', $start), date('w', $end));
$x = floor(($end-$start)/ONE_WEEK);
$sum = 0;
for ($day = 0;$day < 7;++$day) {
if ($days & pow(2, $day)) {
$sum += $x + ($w[0] > $w[1]?$w[0] <= $day || $day <= $w[1] : $w[0] <= $day && $day <= $w[1]);
}
}
return $sum;
}
//$start = $end = time();
// 0x10 == pow(2, 4) == 1 << 4 // THURSDAY
// 0x20 == pow(2, 5) == 1 << 5 // FRIDAY
echo number_of_days(0x01, $start, $end); // SUNDAY
echo number_of_days(pow(2, 0), $start, $end); // SUNDAY
echo number_of_days(0x02, $start, $end); // MONDAY
echo number_of_days(pow(2, 1), $start, $end); // MONDAY
echo number_of_days(0x04, $start, $end); // TUESDAY
echo number_of_days(1 << 2, $start, $end); // TUESDAY
echo number_of_days(0x08, $start, $end); // WEDNESDAY
echo number_of_days(1 << 3, $start, $end); // WEDNESDAY
echo number_of_days(0x10, $start, $end); // THURSDAY
echo number_of_days(0x20, $start, $end); // FRIDAY
echo number_of_days(0x40, $start, $end); // SATURDAY
echo number_of_days(0x01 | 0x40, $start, $end); // WEEKENDS : SUNDAY | SATURDAY
echo number_of_days(0x3E, $start, $end); // WORKDAYS : MONDAY | TUESDAY | WEDNESDAY | THURSDAY | FRIDAY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment