• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add X days to current date and calculate excluding saturdays and sundays

#1
[eluser]ninjayan[/eluser]
Hello everyone!
I am currently developing a document tracking application which will calculate the duration of document.

So the number of days to be added from the current date (document creation) is set from the database (dynamic).

I have this code but this will not make it dynamic since the number of days to be added is hard coded.

Code:
$add = 5;
if ($add == "5") {
$xa = date("Y-m-d", strtotime("+7 days"));
}elseif ($add == "10") {
$xa = date("Y-m-d", strtotime("+14 days"));
}

echo $xa;

So the code above is just to illustrate what I want to achieve.

#2
[eluser]ninjayan[/eluser]
If the admin updates the number of days from the database for example from the code given. 5 will becomes ten.
But the problem is the fixed +7 +14 5 10. That makes it not dynamic.

#3
[eluser]benton.snyder[/eluser]
Code:
$add = $database->result(); // fetch the number of days stored in the database
if(is_int($add) && $add>-1) // ensure number of days is a positive integer
     echo date("Y-m-d", strtotime("+{$add} days")); // calculate and print

#4
[eluser]ninjayan[/eluser]
will this exclude saturdays and sundays? thanks for your input. appreciated.
beginner here

#5
[eluser]benton.snyder[/eluser]
That's considerably more complicated. Something like the following should do the trick though.

Code:
$today = new DateTime();
switch(date_format($today, "l"))                        // assumes start date falls on a weekday
{
        case 'Monday':
                $weekdays_left = 4;
                break;
        case 'Tuesday':
                $weekdays_left = 3;
                break;
        case 'Wednesday':
                $weekdays_left = 2;
                break;
        case 'Thursday':
                $weekdays_left = 1;
                break;
        case 'Friday':
                $weekdays_left = 0;
                break;
}

$database_result = 10;                                  // replace with actual database result
$days_to_add = $database_result;
if(is_int($days_to_add) && $days_to_add > 0)            // ensure days to add is positive integer
{
        $coeff = floor($days_to_add / 7);
        if($coeff > 0)                                  // if days to add is greater than or equal to 1 week
        {
                $add = $days_to_add + $coeff*2;
                if($days_to_add % 7 > $weekdays_left)
                        $add += 2;
        }
        else                                            // if days to add is less than 1 week
        {
                $add = $days_to_add;
                if($days_to_add > $weekdays_left)       // target date falls on weekday of next week
                        $add += 2;
        }

        echo date("Y-m-d", strtotime("+{$add} days"));
}

#6
[eluser]ninjayan[/eluser]
Thank you. I appreciate your effort. I also found this code but not so sure thou.

Code:
$startDate = date("M d, Y");// current date
$endDate = $startDate;
$numDays = 10;
$counter = 1;
while ($counter <= $numDays) {

   $endDate = date("M d, Y", strtotime(date("Y-m-d", strtotime($endDate)) . " +1 day"));
   $dayOfTheWeek = date("l",strtotime($endDate));

if ($dayOfTheWeek == "Saturday" || $dayOfTheWeek == "Sunday") {
       continue;
   }else {
      $counter++;
   }

}

echo "startDate: $startDate<br />";
echo "endDate: $endDate<br />";

Your code also answered my problem. thanks again.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.