0

I have the following code calculating the total amount of business days: (working)

<?php
//get current month for example
if(!isset($_GET['from'])) {
    $beginday=date("Y-m-01");
} else {
    $beginday=date($_GET['from']);
}
if(!isset($_GET['to'])) {
    $lastday=date("Y-m-01");
} else {
    $lastday=date($_GET['to']);
}

$nr_work_days = getWorkingDays($beginday,$lastday);
echo $nr_work_days;

function getWorkingDays($startDate, $endDate){
 $begin=strtotime($startDate);
 $end=strtotime($endDate);
 if($begin>$end){
  echo "startdate is in the future! <br />";
  return 0;
 }else{
   $no_days=0;
   $weekends=0;
  while($begin<=$end){
    $no_days++; // no of days in the given interval
    $what_day=date("N",$begin);
     if($what_day>5) { // 6 and 7 are weekend days
          $weekends++;
     };
    $begin+=86400; // +1 day
  };
  $working_days=$no_days-$weekends;
  return $working_days;
 }
}
?>

What i miss now is the feature that cuts out all the public holidays. Any suggestions whats the best way to perform this?

Jonathan
  • 1,955
  • 5
  • 30
  • 50
  • 1
    PHP doesn't know _or care_ when public holidays are (they're different for different countries for one thing) so you will need to tell PHP which days to ignore manually. – naththedeveloper Jul 29 '14 at 08:08
  • As far as i know its not depending to any programming language to get the public holidays since you can get the easter-date to get all other public holidays. What i miss is the skill to perform thatl. – Jonathan Jul 29 '14 at 08:10
  • Get the easter-date? You know Easter can be on a different date in different countries, right? And that some countries just have occasional extra public holidays? We had an extra one in 2012 for the [Queen's Diamond Jubilee](http://www.bbc.co.uk/news/magazine-18277486). Next month Scotland will have its Summer Bank Holiday on the 4th; England will have it on the 25th. You may need to give this some careful consideration, depending on who you're writing this code for. – Matt Gibson Jul 29 '14 at 08:19
  • Nothing about different countries: http://php.net/manual/de/function.easter-date.php Please explain.. I know about the other holidays to depend on the local country but the easter sunday itself? never heard about that – Jonathan Jul 29 '14 at 08:58
  • @xcy7e Well, in Greece, for example, Easter may be a different date from the one calculated by that function, because they use the Greek Orthodox calendar. Also, of course, there are countries that just don't have a public holiday for Easter. And each country has its own public holidays that aren't shared with other countries. Holidays are, frankly, very complicated, and aren't something you can simply calculate if you've got an international audience. – Matt Gibson Jul 29 '14 at 13:55

1 Answers1

1

You can use,

//Subtract the holidays
foreach($holidays as $holiday){
    $time_stamp=strtotime($holiday);
    //If the holiday doesn't fall in weekend
    if ($startDate <= $time_stamp && $time_stamp <= $endDate && date("N",$time_stamp) != 6 && date("N",$time_stamp) != 7)
        $working_days--;
}

Note: $holidays is an array of all of yours holidays date.

Reference: Calculate business days

Community
  • 1
  • 1
Rikesh
  • 26,156
  • 14
  • 79
  • 87
  • But this would not consider the date-range would it? For example if the holiday is outside the given date-range and its not at the weekend it'd still decrement the working-days result, true? – Jonathan Jul 29 '14 at 08:14
  • No it won't,see answer carefully. It has condition which compare holiday date with `$startDate` & `$endDate`. – Rikesh Jul 29 '14 at 08:20
  • the above just calculates weekdays only. I understand a solution is needed where it includes the 'holidays' – Patrick Mutwiri Dec 07 '15 at 06:46