Working Days (Mon-Fri) in PHP

Working days (Mon-Fri) in PHP

If you are limiting to weekdays use the string weekdays.

echo date ( 'Y-m-j' , strtotime ( '3 weekdays' ) );

This should jump you ahead by 3 weekdays, so if it is Thursday it will add the additional weekend time.

Source: http://www.php.net/manual/en/datetime.formats.relative.php

Calculate business days

Here's a function from the user comments on the date() function page in the PHP manual. It's an improvement of an earlier function in the comments that adds support for leap years.

Enter the starting and ending dates, along with an array of any holidays that might be in between, and it returns the working days as an integer:

<?php
//The function returns the no. of business days between two dates and it skips the holidays
function getWorkingDays($startDate,$endDate,$holidays){
// do strtotime calculations just once
$endDate = strtotime($endDate);
$startDate = strtotime($startDate);

//The total number of days between the two dates. We compute the no. of seconds and divide it to 60*60*24
//We add one to inlude both dates in the interval.
$days = ($endDate - $startDate) / 86400 + 1;

$no_full_weeks = floor($days / 7);
$no_remaining_days = fmod($days, 7);

//It will return 1 if it's Monday,.. ,7 for Sunday
$the_first_day_of_week = date("N", $startDate);
$the_last_day_of_week = date("N", $endDate);

//---->The two can be equal in leap years when february has 29 days, the equal sign is added here
//In the first case the whole interval is within a week, in the second case the interval falls in two weeks.
if ($the_first_day_of_week <= $the_last_day_of_week) {
if ($the_first_day_of_week <= 6 && 6 <= $the_last_day_of_week) $no_remaining_days--;
if ($the_first_day_of_week <= 7 && 7 <= $the_last_day_of_week) $no_remaining_days--;
}
else {
// (edit by Tokes to fix an edge case where the start day was a Sunday
// and the end day was NOT a Saturday)

// the day of the week for start is later than the day of the week for end
if ($the_first_day_of_week == 7) {
// if the start date is a Sunday, then we definitely subtract 1 day
$no_remaining_days--;

if ($the_last_day_of_week == 6) {
// if the end date is a Saturday, then we subtract another day
$no_remaining_days--;
}
}
else {
// the start date was a Saturday (or earlier), and the end date was (Mon..Fri)
// so we skip an entire weekend and subtract 2 days
$no_remaining_days -= 2;
}
}

//The no. of business days is: (number of weeks between the two dates) * (5 working days) + the remainder
//---->february in none leap years gave a remainder of 0 but still calculated weekends between first and last day, this is one way to fix it
$workingDays = $no_full_weeks * 5;
if ($no_remaining_days > 0 )
{
$workingDays += $no_remaining_days;
}

//We 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)
$workingDays--;
}

return $workingDays;
}

//Example:

$holidays=array("2008-12-25","2008-12-26","2009-01-01");

echo getWorkingDays("2008-12-22","2009-01-02",$holidays)
// => will return 7
?>

Php add 5 working days to current date excluding weekends (sat-sun) and excluding (multiple) holidays

You can use the "while statement", looping until get enough 5 days. Each time looping get & check one next day is in the holiday list or not.

Here is the the example:

$holidayDates = array(
'2016-03-26',
'2016-03-27',
'2016-03-28',
'2016-03-29',
'2016-04-05',
);

$count5WD = 0;
$temp = strtotime("2016-03-25 00:00:00"); //example as today is 2016-03-25
while($count5WD<5){
$next1WD = strtotime('+1 weekday', $temp);
$next1WDDate = date('Y-m-d', $next1WD);
if(!in_array($next1WDDate, $holidayDates)){
$count5WD++;
}
$temp = $next1WD;
}

$next5WD = date("Y-m-d", $temp);

echo $next5WD; //if today is 2016-03-25 then it will return 2016-04-06 as many days between are holidays

Calculate number of working days in a month

function countDays($year, $month, $ignore) {
$count = 0;
$counter = mktime(0, 0, 0, $month, 1, $year);
while (date("n", $counter) == $month) {
if (in_array(date("w", $counter), $ignore) == false) {
$count++;
}
$counter = strtotime("+1 day", $counter);
}
return $count;
}
echo countDays(2013, 1, array(0, 6)); // 23

The date function is used in this example. Note about ignore parameter: 0 is sunday, ..., 6 is saturday.

Finding last 7 working days in PHP

Finding holidays will be more complicated.
One solution can be like saving the holidays in advance and skip them if they comes in the condition.

One simple solution for your problem can be like this.

<?php

$holiday = array(
'2017-12-16' => 'Victory Day of Bangladesh',
'2017-12-25' => 'Christmas'
);

$i = 0;
$work_day = '2017-12-26';
while($i != 7)
{
$work_day = date('Y-m-d', strtotime('-1 day', strtotime($work_day)));
$day_name = date('l', strtotime($work_day));

if($day_name != 'Saturday' && $day_name != 'Sunday' && !isset($holiday[$work_day]))
{
echo $work_day."\n";
$i++;
}
}

?>

PHP calculate a date based on function for working days

I did a similar thing a while back using the below function. the key here is skipping the weekends, you can extend this to skip holidays as well.

Example:

Call the function - > addDays(strtotime($startDate), 20, $skipdays,$skipdates = array())

 <?php
function addDays($timestamp, $days, $skipdays = array("Saturday", "Sunday"), $skipdates = NULL) {
// $skipdays: array (Monday-Sunday) eg. array("Saturday","Sunday")
// $skipdates: array (YYYY-mm-dd) eg. array("2012-05-02","2015-08-01");
//timestamp is strtotime of ur $startDate
$i = 1;

while ($days >= $i) {
$timestamp = strtotime("+1 day", $timestamp);
if ( (in_array(date("l", $timestamp), $skipdays)) || (in_array(date("Y-m-d", $timestamp), $skipdates)) )
{
$days++;
}
$i++;
}

return $timestamp;
//return date("m/d/Y",$timestamp);
}
?>

[Edit] : Just read an amazing article on nettuts, Hope this helps http://net.tutsplus.com/tutorials/php/dates-and-time-the-oop-way/

Get number of weekdays in a given month

Some basic code:

$month = 12;
$weekdays = array();
$d = 1;

do {
$mk = mktime(0, 0, 0, $month, $d, date("Y"));
@$weekdays[date("w", $mk)]++;
$d++;
} while (date("m", $mk) == $month);

print_r($weekdays);

Remove the @ if your PHP error warning doesn't show notices.

Next business day of given date in PHP

Next Weekday

This finds the next weekday from a specific date (not including Saturday or Sunday):

echo date('Y-m-d', strtotime('2011-04-05 +1 Weekday'));

You could also do it with a date variable of course:

$myDate = '2011-04-05';
echo date('Y-m-d', strtotime($myDate . ' +1 Weekday'));

UPDATE: Or, if you have access to PHP's DateTime class (very likely):

$date = new DateTime('2018-01-27');
$date->modify('+7 weekday');
echo $date->format('Y-m-d');

Want to Skip Holidays?:

Although the original poster mentioned "I don't need to consider holidays", if you DO happen to want to ignore holidays, just remember - "Holidays" is just an array of whatever dates you don't want to include and differs by country, region, company, person...etc.

Simply put the above code into a function that excludes/loops past the dates you don't want included. Something like this:

$tmpDate = '2015-06-22';
$holidays = ['2015-07-04', '2015-10-31', '2015-12-25'];
$i = 1;
$nextBusinessDay = date('Y-m-d', strtotime($tmpDate . ' +' . $i . ' Weekday'));

while (in_array($nextBusinessDay, $holidays)) {
$i++;
$nextBusinessDay = date('Y-m-d', strtotime($tmpDate . ' +' . $i . ' Weekday'));
}

I'm sure the above code can be simplified or shortened if you want. I tried to write it in an easy-to-understand way.



Related Topics



Leave a reply



Submit