0

I have a situation like this. I have a string stored in DB, like this:

$string: 1,1,1,1,1,1,0 

It means: Sunday is 0 (a holiday), all the other days (Mon...Sat) are 1.

Then I have two dates: today (ex: 2012-09-07) and some end date (2012-10-04).

I wonder what should I do to get the total number of business days (deducting the holidays) between Today and this End Date (23 days in my example)? We should use the string stored in the DB.

raina77ow
  • 103,633
  • 15
  • 192
  • 229
jphp_dev
  • 11
  • 1
  • 1
  • 1
    can you please include your code, it is hard to follow you. – jtheman Sep 07 '12 at 09:42
  • 1
    have a look here: http://stackoverflow.com/questions/336127/calculate-business-days , it looks more promissing than your approach with 1,1,1,1,1,1,0 – Najzero Sep 07 '12 at 09:44
  • Thank jtheman. I am thinking solution for this problem but I can not. So I have not code. – jphp_dev Sep 07 '12 at 09:45
  • I found this answer on stackover but it don't meet my requirement. My problem no put $holidays=array("2008-12-25","2008-12-26","2009-01-01"); My problem is during the period from today to end date compare $string 1,1,1,1,1,1,0 and auto sub 1 when match Sunday – jphp_dev Sep 07 '12 at 09:47

2 Answers2

2
$workdays = explode(",", "1,1,1,1,1,1,0"); // turn string to array (Monday to Sunday)
$days = 0;
$time = strtotime("2012-09-07");
$end_time = strtotime("2012-10-04");
while ($time < $end_time)
    {
        $day_of_week = date("N", $time) - 1; // 0 = Monday, 6 = Sunday
        if ($workdays[$day_of_week] == 1)
            {
                $days++;
            }
        $time = strtotime("+1 day", $time); // add one day to time
    }

This should work for you. You may need to modify it based on whether it's inclusive, when it should start, etc.

Eligos
  • 1,104
  • 1
  • 11
  • 26
0

Have you tried date_diff() ? http://php.net/manual/en/function.date-diff.php

Alexandru R
  • 8,560
  • 16
  • 64
  • 98
  • I don't think this will provide the exact output he desires .. he wants to deduct Sundays from the calculations, this means 6 days/week not 7 – sikas Sep 07 '12 at 09:45