I need a method for adding "business days" in PHP. For example, Friday 12/5 + 3 business days = Wednesday 12/10.
At a minimum I need the code to understand weekends, but ideally it should account for US federal holidays as well. I'm sure I could come up with a solution by brute force if necessary, but I'm hoping there's a more elegant approach out there. Anyone?
Thanks.
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
?>
Get the number of working days without holidays between two dates :
Use example:
echo number_of_working_days('2013-12-23', '2013-12-29');
Output:
3
Function:
function number_of_working_days($from, $to) {
$workingDays = [1, 2, 3, 4, 5]; # date format = N (1 = Monday, ...)
$holidayDays = ['*-12-25', '*-01-01', '2013-12-23']; # variable and fixed holidays
$from = new DateTime($from);
$to = new DateTime($to);
$to->modify('+1 day');
$interval = new DateInterval('P1D');
$periods = new DatePeriod($from, $interval, $to);
$days = 0;
foreach ($periods as $period) {
if (!in_array($period->format('N'), $workingDays)) continue;
if (in_array($period->format('Y-m-d'), $holidayDays)) continue;
if (in_array($period->format('*-m-d'), $holidayDays)) continue;
$days++;
}
return $days;
}
There are some args for the date() function that should help. If you check date("w") it will give you a number for the day of the week, from 0 for Sunday through 6 for Saturday. So.. maybe something like..
$busDays = 3;
$day = date("w");
if( $day > 2 && $day <= 5 ) { /* if between Wed and Fri */
$day += 2; /* add 2 more days for weekend */
}
$day += $busDays;
This is just a rough example of one possibility..
$startDate = new DateTime( '2013-04-01' ); //intialize start date
$endDate = new DateTime( '2013-04-30' ); //initialize end date
$holiday = array('2013-04-11','2013-04-25'); //this is assumed list of holiday
$interval = new DateInterval('P1D'); // set the interval as 1 day
$daterange = new DatePeriod($startDate, $interval ,$endDate);
foreach($daterange as $date){
if($date->format("N") <6 AND !in_array($date->format("Y-m-d"),$holiday))
$result[] = $date->format("Y-m-d");
}
echo "<pre>";print_r($result);
Holiday calculation is non-standard in each State. I am writing a bank application which I need some hard business rules for but can still only get a rough standard.
/**
* National American Holidays
* #param string $year
* #return array
*/
public static function getNationalAmericanHolidays($year) {
// January 1 - New Year’s Day (Observed)
// Calc Last Monday in May - Memorial Day strtotime("last Monday of May 2011");
// July 4 Independence Day
// First monday in september - Labor Day strtotime("first Monday of September 2011")
// November 11 - Veterans’ Day (Observed)
// Fourth Thursday in November Thanksgiving strtotime("fourth Thursday of November 2011");
// December 25 - Christmas Day
$bankHolidays = array(
$year . "-01-01" // New Years
, "". date("Y-m-d",strtotime("last Monday of May " . $year) ) // Memorial Day
, $year . "-07-04" // Independence Day (corrected)
, "". date("Y-m-d",strtotime("first Monday of September " . $year) ) // Labor Day
, $year . "-11-11" // Veterans Day
, "". date("Y-m-d",strtotime("fourth Thursday of November " . $year) ) // Thanksgiving
, $year . "-12-25" // XMAS
);
return $bankHolidays;
}
Here is a function for adding buisness days to a date
function add_business_days($startdate,$buisnessdays,$holidays,$dateformat){
$i=1;
$dayx = strtotime($startdate);
while($i < $buisnessdays){
$day = date('N',$dayx);
$date = date('Y-m-d',$dayx);
if($day < 6 && !in_array($date,$holidays))$i++;
$dayx = strtotime($date.' +1 day');
}
return date($dateformat,$dayx);
}
//Example
date_default_timezone_set('Europe\London');
$startdate = '2012-01-08';
$holidays=array("2012-01-10");
echo '<p>Start date: '.date('r',strtotime( $startdate));
echo '<p>'.add_business_days($startdate,7,$holidays,'r');
Another post mentions getWorkingDays (from php.net comments and included here) but I think it breaks if you start on a Sunday and finish on a work day.
Using the following (you'll need to include the getWorkingDays function from previous post)
date_default_timezone_set('Europe\London');
//Example:
$holidays = array('2012-01-10');
$startDate = '2012-01-08';
$endDate = '2012-01-13';
echo getWorkingDays( $startDate,$endDate,$holidays);
Gives the result as 5 not 4
Sun, 08 Jan 2012 00:00:00 +0000 weekend
Mon, 09 Jan 2012 00:00:00 +0000
Tue, 10 Jan 2012 00:00:00 +0000 holiday
Wed, 11 Jan 2012 00:00:00 +0000
Thu, 12 Jan 2012 00:00:00 +0000
Fri, 13 Jan 2012 00:00:00 +0000
The following function was used to generate the above.
function get_working_days($startDate,$endDate,$holidays){
$debug = true;
$work = 0;
$nowork = 0;
$dayx = strtotime($startDate);
$endx = strtotime($endDate);
if($debug){
echo '<h1>get_working_days</h1>';
echo 'startDate: '.date('r',strtotime( $startDate)).'<br>';
echo 'endDate: '.date('r',strtotime( $endDate)).'<br>';
var_dump($holidays);
echo '<p>Go to work...';
}
while($dayx <= $endx){
$day = date('N',$dayx);
$date = date('Y-m-d',$dayx);
if($debug)echo '<br />'.date('r',$dayx).' ';
if($day > 5 || in_array($date,$holidays)){
$nowork++;
if($debug){
if($day > 5)echo 'weekend';
else echo 'holiday';
}
} else $work++;
$dayx = strtotime($date.' +1 day');
}
if($debug){
echo '<p>No work: '.$nowork.'<br>';
echo 'Work: '.$work.'<br>';
echo 'Work + no work: '.($nowork+$work).'<br>';
echo 'All seconds / seconds in a day: '.floatval(strtotime($endDate)-strtotime($startDate))/floatval(24*60*60);
}
return $work;
}
date_default_timezone_set('Europe\London');
//Example:
$holidays=array("2012-01-10");
$startDate = '2012-01-08';
$endDate = '2012-01-13';
//broken
echo getWorkingDays( $startDate,$endDate,$holidays);
//works
echo get_working_days( $startDate,$endDate,$holidays);
Bring on the holidays...
You can try this function which is more simple.
function getWorkingDays($startDate, $endDate)
{
$begin = strtotime($startDate);
$end = strtotime($endDate);
if ($begin > $end) {
return 0;
} else {
$no_days = 0;
while ($begin <= $end) {
$what_day = date("N", $begin);
if (!in_array($what_day, [6,7]) ) // 6 and 7 are weekend
$no_days++;
$begin += 86400; // +1 day
};
return $no_days;
}
}
My version based on the work by #mcgrailm... tweaked because the report needed to be reviewed within 3 business days, and if submitted on a weekend, the counting would start on the following Monday:
function business_days_add($start_date, $business_days, $holidays = array()) {
$current_date = strtotime($start_date);
$business_days = intval($business_days); // Decrement does not work on strings
while ($business_days > 0) {
if (date('N', $current_date) < 6 && !in_array(date('Y-m-d', $current_date), $holidays)) {
$business_days--;
}
if ($business_days > 0) {
$current_date = strtotime('+1 day', $current_date);
}
}
return $current_date;
}
And working out the difference of two dates in terms of business days:
function business_days_diff($start_date, $end_date, $holidays = array()) {
$business_days = 0;
$current_date = strtotime($start_date);
$end_date = strtotime($end_date);
while ($current_date <= $end_date) {
if (date('N', $current_date) < 6 && !in_array(date('Y-m-d', $current_date), $holidays)) {
$business_days++;
}
if ($current_date <= $end_date) {
$current_date = strtotime('+1 day', $current_date);
}
}
return $business_days;
}
As a note, everyone who is using 86400, or 24*60*60, please don't... your forgetting time changes from winter/summer time, where a day it not exactly 24 hours. While it's a little slower the strtotime('+1 day', $timestamp), it much more reliable.
A function to add or subtract business days from a given date, this doesn't account for holidays.
function dateFromBusinessDays($days, $dateTime=null) {
$dateTime = is_null($dateTime) ? time() : $dateTime;
$_day = 0;
$_direction = $days == 0 ? 0 : intval($days/abs($days));
$_day_value = (60 * 60 * 24);
while($_day !== $days) {
$dateTime += $_direction * $_day_value;
$_day_w = date("w", $dateTime);
if ($_day_w > 0 && $_day_w < 6) {
$_day += $_direction * 1;
}
}
return $dateTime;
}
use like so...
echo date("m/d/Y", dateFromBusinessDays(-7));
echo date("m/d/Y", dateFromBusinessDays(3, time() + 3*60*60*24));
Brute attempt to detect working time - Monday to Friday 8am-4pm:
if (date('N')<6 && date('G')>8 && date('G')<16) {
// we have a working time (or check for holidays)
}
Below is the working code to calculate working business days from a given date.
<?php
$holiday_date_array = array("2016-01-26", "2016-03-07", "2016-03-24", "2016-03-25", "2016-04-15", "2016-08-15", "2016-09-12", "2016-10-11", "2016-10-31");
$date_required = "2016-03-01";
function increase_date($date_required, $holiday_date_array=array(), $days = 15){
if(!empty($date_required)){
$counter_1=0;
$incremented_date = '';
for($i=1; $i <= $days; $i++){
$date = strtotime("+$i day", strtotime($date_required));
$day_name = date("D", $date);
$incremented_date = date("Y-m-d", $date);
if($day_name=='Sat'||$day_name=='Sun'|| in_array($incremented_date ,$holiday_date_array)==true){
$counter_1+=1;
}
}
if($counter_1 > 0){
return increase_date($incremented_date, $holiday_date_array, $counter_1);
}else{
return $incremented_date;
}
}else{
return 'invalid';
}
}
echo increase_date($date_required, $holiday_date_array, 15);
?>
//output after adding 15 business working days in 2016-03-01 will be "2016-03-23"
This code snippet is very easy to calculate business day without week end and holidays:
function getWorkingDays($startDate,$endDate,$offdays,$holidays){
$endDate = strtotime($endDate);
$startDate = strtotime($startDate);
$days = ($endDate - $startDate) / 86400 + 1;
$counter=0;
for ($i = 1; $i <= $days; $i++) {
$the_first_day_of_week = date("N", $startDate);
$startDate+=86400;
if (!in_array($the_first_day_of_week, $offdays) && !in_array(date("Y-m-
d",$startDate), $holidays)) {
$counter++;
}
}
return $counter;
}
//example to use
$holidays=array("2017-07-03","2017-07-20");
$offdays=array(5,6);//weekend days Monday=1 .... Sunday=7
echo getWorkingDays("2017-01-01","2017-12-31",$offdays,$holidays)
Here is another solution without for loop for each day.
$from = new DateTime($first_date);
$to = new DateTime($second_date);
$to->modify('+1 day');
$interval = $from->diff($to);
$days = $interval->format('%a');
$extra_days = fmod($days, 7);
$workdays = ( ( $days - $extra_days ) / 7 ) * 5;
$first_day = date('N', strtotime($first_date));
$last_day = date('N', strtotime("1 day", strtotime($second_date)));
$extra = 0;
if($first_day > $last_day) {
if($first_day == 7) {
$first_day = 6;
}
$extra = (6 - $first_day) + ($last_day - 1);
if($extra < 0) {
$extra = $extra * -1;
}
}
if($last_day > $first_day) {
$extra = $last_day - $first_day;
}
$days = $workdays + $extra
For holidays, make an array of days in some format that date() can produce. Example:
// I know, these aren't holidays
$holidays = array(
'Jan 2',
'Feb 3',
'Mar 5',
'Apr 7',
// ...
);
Then use the in_array() and date() functions to check if the timestamp represents a holiday:
$day_of_year = date('M j', $timestamp);
$is_holiday = in_array($day_of_year, $holidays);
I had this same need i started with bobbin's first example and ended up with this
function add_business_days($startdate,$buisnessdays,$holidays=array(),$dateformat){
$enddate = strtotime($startdate);
$day = date('N',$enddate);
while($buisnessdays > 1){
$enddate = strtotime(date('Y-m-d',$enddate).' +1 day');
$day = date('N',$enddate);
if($day < 6 && !in_array($enddate,$holidays))$buisnessdays--;
}
return date($dateformat,$enddate);
}
hth someone
Variant 1:
<?php
/*
* Does not count current day, the date returned is the last business day
* Requires PHP 5.1 (Using ISO-8601 week)
*/
function businessDays($timestamp = false, $bDays = 2) {
if($timestamp === false) $timestamp = time();
while ($bDays>0) {
$timestamp += 86400;
if (date('N', $timestamp)<6) $bDays--;
}
return $timestamp;
}
Variant 2:
<?php
/*
* Does not count current day, the date returned is a business day
* following the last business day
* Requires PHP 5.1 (Using ISO-8601 week)
*/
function businessDays($timestamp = false, $bDays = 2) {
if($timestamp === false) $timestamp = time();
while ($bDays+1>0) {
$timestamp += 86400;
if (date('N', $timestamp)<6) $bDays--;
}
return $timestamp;
}
Variant 3:
<?php
/*
* Does not count current day, the date returned is
* a date following the last business day (can be weekend or not.
* See above for alternatives)
* Requires PHP 5.1 (Using ISO-8601 week)
*/
function businessDays($timestamp = false, $bDays = 2) {
if($timestamp === false) $timestamp = time();
while ($bDays>0) {
$timestamp += 86400;
if (date('N', $timestamp)<6) $bDays--;
}
return $timestamp += 86400;
}
The additional holiday considerations can be made using variations of the above by doing the following. Note! assure all the timestamps are the same time of the day (i.e. midnight).
Make an array of holiday dates (as unixtimestamps) i.e.:
$holidays = array_flip(strtotime('2011-01-01'),strtotime('2011-12-25'));
Modify line :
if (date('N', $timestamp)<6) $bDays--;
to be :
if (date('N', $timestamp)<6 && !isset($holidays[$timestamp])) $bDays--;
Done!
<?php
/*
* Does not count current day, the date returned is the last business day
* Requires PHP 5.1 (Using ISO-8601 week)
*/
function businessDays($timestamp = false, $bDays = 2) {
if($timestamp === false) $timestamp = strtotime(date('Y-m-d',time()));
$holidays = array_flip(strtotime('2011-01-01'),strtotime('2011-12-25'));
while ($bDays>0) {
$timestamp += 86400;
if (date('N', $timestamp)<6 && !isset($holidays[$timestamp])) $bDays--;
}
return $timestamp;
}
<?php
function AddWorkDays(){
$i = 0;
$d = 5; // Number of days to add
while($i <= $d) {
$i++;
if(date('N', mktime(0, 0, 0, date(m), date(d)+$i, date(Y))) < 5) {
$d++;
}
}
return date(Y).','.date(m).','.(date(d)+$d);
}
?>
Here is a recursive solution. It can easily be modified to only keep track of and return the latest date.
// Returns a $numBusDays-sized array of all business dates,
// starting from and including $currentDate.
// Any date in $holidays will be skipped over.
function getWorkingDays($currentDate, $numBusDays, $holidays = array(),
$resultDates = array())
{
// exit when we have collected the required number of business days
if ($numBusDays === 0) {
return $resultDates;
}
// add current date to return array, if not a weekend or holiday
$date = date("w", strtotime($currentDate));
if ( $date != 0 && $date != 6 && !in_array($currentDate, $holidays) ) {
$resultDates[] = $currentDate;
$numBusDays -= 1;
}
// set up the next date to test
$currentDate = new DateTime("$currentDate + 1 day");
$currentDate = $currentDate->format('Y-m-d');
return getWorkingDays($currentDate, $numBusDays, $holidays, $resultDates);
}
// test
$days = getWorkingDays('2008-12-05', 4);
print_r($days);
date_default_timezone_set('America/New_York');
/** Given a number days out, what day is that when counting by 'business' days
* get the next business day. by default it looks for next business day
* ie calling $date = get_next_busines_day(); on monday will return tuesday
* $date = get_next_busines_day(2); on monday will return wednesday
* $date = get_next_busines_day(2); on friday will return tuesday
*
* #param $number_of_business_days (integer) how many business days out do you want
* #param $start_date (string) strtotime parseable time value
* #param $ignore_holidays (boolean) true/false to ignore holidays
* #param $return_format (string) as specified in php.net/date
*/
function get_next_business_day($number_of_business_days=1,$start_date='today',$ignore_holidays=false,$return_format='m/d/y') {
// get the start date as a string to time
$result = strtotime($start_date);
// now keep adding to today's date until number of business days is 0 and we land on a business day
while ($number_of_business_days > 0) {
// add one day to the start date
$result = strtotime(date('Y-m-d',$result) . " + 1 day");
// this day counts if it's a weekend and not a holiday, or if we choose to ignore holidays
if (is_weekday(date('Y-m-d',$result)) && (!(is_holiday(date('Y-m-d',$result))) || $ignore_holidays) )
$number_of_business_days--;
}
// when my $number of business days is exausted I have my final date
return(date($return_format,$result));
}
function is_weekend($date) {
// return if this is a weekend date or not.
return (date('N', strtotime($date)) >= 6);
}
function is_weekday($date) {
// return if this is a weekend date or not.
return (date('N', strtotime($date)) < 6);
}
function is_holiday($date) {
// return if this is a holiday or not.
// what are my holidays for this year
$holidays = array("New Year's Day 2011" => "12/31/10",
"Good Friday" => "04/06/12",
"Memorial Day" => "05/28/12",
"Independence Day" => "07/04/12",
"Floating Holiday" => "12/31/12",
"Labor Day" => "09/03/12",
"Thanksgiving Day" => "11/22/12",
"Day After Thanksgiving Day" => "11/23/12",
"Christmas Eve" => "12/24/12",
"Christmas Day" => "12/25/12",
"New Year's Day 2012" => "01/02/12",
"New Year's Day 2013" => "01/01/13"
);
return(in_array(date('m/d/y', strtotime($date)),$holidays));
}
print get_next_business_day(1) . "\n";
<?php
// $today is the UNIX timestamp for today's date
$today = time();
echo "<strong>Today is (ORDER DATE): " . '<font color="red">' . date('l, F j, Y', $today) . "</font></strong><br/><br/>";
//The numerical representation for day of week (Ex. 01 for Monday .... 07 for Sunday
$today_numerical = date("N",$today);
//leadtime_days holds the numeric value for the number of business days
$leadtime_days = $_POST["leadtime"];
//leadtime is the adjusted date for shipdate
$shipdate = time();
while ($leadtime_days > 0)
{
if ($today_numerical != 5 && $today_numerical != 6)
{
$shipdate = $shipdate + (60*60*24);
$today_numerical = date("N",$shipdate);
$leadtime_days --;
}
else
$shipdate = $shipdate + (60*60*24);
$today_numerical = date("N",$shipdate);
}
echo '<strong>Estimated Ship date: ' . '<font color="green">' . date('l, F j, Y', $shipdate) . "</font></strong>";
?>
calculate workdays between two dates including holidays and custom workweek
The answer is not that trivial - thus my suggestion would be to use a class where you can configure more than relying on simplistic function (or assuming a fixed locale and culture). To get the date after a certain number of workdays you'll:
need to specify what weekdays you'll be working (default to MON-FRI) - the class allows you to enable or disable each weekday individually.
need to know that you need to consider public holidays (country and state) to be accurate
e.g. https://github.com/khatfield/php-HolidayLibrary/blob/master/Holidays.class.php
or hardcode the data: e.g. from http://www.feiertagskalender.ch/?hl=en
or pay for data-API http://www.timeanddate.com/services/api/holiday-api.html
Functional Approach
/**
* #param days, int
* #param $format, string: dateformat (if format defined OTHERWISE int: timestamp)
* #param start, int: timestamp (mktime) default: time() //now
* #param $wk, bit[]: flags for each workday (0=SUN, 6=SAT) 1=workday, 0=day off
* #param $holiday, string[]: list of dates, YYYY-MM-DD, MM-DD
*/
function working_days($days, $format='', $start=null, $week=[0,1,1,1,1,1,0], $holiday=[])
{
if(is_null($start)) $start = time();
if($days <= 0) return $start;
if(count($week) != 7) trigger_error('workweek must contain bit-flags for 7 days');
if(array_sum($week) == 0) trigger_error('workweek must contain at least one workday');
$wd = date('w', $start);//0=sun, 6=sat
$time = $start;
while($days)
{
if(
$week[$wd]
&& !in_array(date('Y-m-d', $time), $holiday)
&& !in_array(date('m-d', $time), $holiday)
) --$days; //decrement on workdays
$wd = date('w', $time += 86400); //add one day in seconds
}
$time -= 86400;//include today
return $format ? date($format, $time): $time;
}
//simple usage
$ten_days = working_days(10, 'D F d Y');
echo '<br>ten workingdays (MON-FRI) disregarding holidays: ',$ten_days;
//work on saturdays and add new years day as holiday
$ten_days = working_days(10, 'D F d Y', null, [0,1,1,1,1,1,1], ['01-01']);
echo '<br>ten workingdays (MON-SAT) disregarding holidays: ',$ten_days;
This is another solution, it is nearly 25% faster than checking holidays with in_array:
/**
* Function to calculate the working days between two days, considering holidays.
* #param string $startDate -- Start date of the range (included), formatted as Y-m-d.
* #param string $endDate -- End date of the range (included), formatted as Y-m-d.
* #param array(string) $holidayDates -- OPTIONAL. Array of holidays dates, formatted as Y-m-d. (e.g. array("2016-08-15", "2016-12-25"))
* #return int -- Number of working days.
*/
function getWorkingDays($startDate, $endDate, $holidayDates=array()){
$dateRange = new DatePeriod(new DateTime($startDate), new DateInterval('P1D'), (new DateTime($endDate))->modify("+1day"));
foreach ($dateRange as $dr) { if($dr->format("N")<6){$workingDays[]=$dr->format("Y-m-d");} }
return count(array_diff($workingDays, $holidayDates));
}
I know I'm late to the party, but I use this old set of functions by Marcos J. Montes for figuring out holidays and business days. He took the time to add an algorithm from 1876 for Easter and he added all the major US holidays. This can easily be updated for other countries.
//Usage
$days = 30;
$next_working_date = nextWorkingDay($days, $somedate);
//add date function
function DateAdd($interval, $number, $date) {
$date_time_array = getdate($date);
//die(print_r($date_time_array));
$hours = $date_time_array["hours"];
$minutes = $date_time_array["minutes"];
$seconds = $date_time_array["seconds"];
$month = $date_time_array["mon"];
$day = $date_time_array["mday"];
$year = $date_time_array["year"];
switch ($interval) {
case "yyyy":
$year+=$number;
break;
case "q":
$year+=($number*3);
break;
case "m":
$month+=$number;
break;
case "y":
case "d":
case "w":
$day+=$number;
break;
case "ww":
$day+=($number*7);
break;
case "h":
$hours+=$number;
break;
case "n":
$minutes+=$number;
break;
case "s":
$seconds+=$number;
break;
}
// echo "day:" . $day;
$timestamp= mktime($hours,$minutes,$seconds,$month,$day,$year);
return $timestamp;
}
// the following function get_holiday() is based on the work done by
// Marcos J. Montes
function get_holiday($year, $month, $day_of_week, $week="") {
if ( (($week != "") && (($week > 5) || ($week < 1))) || ($day_of_week > 6) || ($day_of_week < 0) ) {
// $day_of_week must be between 0 and 6 (Sun=0, ... Sat=6); $week must be between 1 and 5
return FALSE;
} else {
if (!$week || ($week == "")) {
$lastday = date("t", mktime(0,0,0,$month,1,$year));
$temp = (date("w",mktime(0,0,0,$month,$lastday,$year)) - $day_of_week) % 7;
} else {
$temp = ($day_of_week - date("w",mktime(0,0,0,$month,1,$year))) % 7;
}
if ($temp < 0) {
$temp += 7;
}
if (!$week || ($week == "")) {
$day = $lastday - $temp;
} else {
$day = (7 * $week) - 6 + $temp;
}
//echo $year.", ".$month.", ".$day . "<br><br>";
return format_date($year, $month, $day);
}
}
function observed_day($year, $month, $day) {
// sat -> fri & sun -> mon, any exceptions?
//
// should check $lastday for bumping forward and $firstday for bumping back,
// although New Year's & Easter look to be the only holidays that potentially
// move to a different month, and both are accounted for.
$dow = date("w", mktime(0, 0, 0, $month, $day, $year));
if ($dow == 0) {
$dow = $day + 1;
} elseif ($dow == 6) {
if (($month == 1) && ($day == 1)) { // New Year's on a Saturday
$year--;
$month = 12;
$dow = 31;
} else {
$dow = $day - 1;
}
} else {
$dow = $day;
}
return format_date($year, $month, $dow);
}
function calculate_easter($y) {
// In the text below, 'intval($var1/$var2)' represents an integer division neglecting
// the remainder, while % is division keeping only the remainder. So 30/7=4, and 30%7=2
//
// This algorithm is from Practical Astronomy With Your Calculator, 2nd Edition by Peter
// Duffett-Smith. It was originally from Butcher's Ecclesiastical Calendar, published in
// 1876. This algorithm has also been published in the 1922 book General Astronomy by
// Spencer Jones; in The Journal of the British Astronomical Association (Vol.88, page
// 91, December 1977); and in Astronomical Algorithms (1991) by Jean Meeus.
$a = $y%19;
$b = intval($y/100);
$c = $y%100;
$d = intval($b/4);
$e = $b%4;
$f = intval(($b+8)/25);
$g = intval(($b-$f+1)/3);
$h = (19*$a+$b-$d-$g+15)%30;
$i = intval($c/4);
$k = $c%4;
$l = (32+2*$e+2*$i-$h-$k)%7;
$m = intval(($a+11*$h+22*$l)/451);
$p = ($h+$l-7*$m+114)%31;
$EasterMonth = intval(($h+$l-7*$m+114)/31); // [3 = March, 4 = April]
$EasterDay = $p+1; // (day in Easter Month)
return format_date($y, $EasterMonth, $EasterDay);
}
function nextWorkingDay($number_days, $start_date = "") {
$day_counter = 0;
$intCounter = 0;
if ($start_date=="") {
$today = mktime(0, 0, 0, date("m") , date("d"), date("Y"));
} else {
$start_time = strtotime($start_date);
$today = mktime(0, 0, 0, date("m", $start_time) , date("d", $start_time), date("Y", $start_time));
}
while($day_counter < $number_days) {
$working_time = DateAdd("d", 1, $today);
$working_date = date("Y-m-d", $working_date);
if (!isWeekend($working_date) && !confirm_holiday(date("Y-m-d", strtotime($working_date))) ) {
$day_counter++;
}
$intCounter++;
$today = $working_time;
if ($intCounter > 1000) {
//just in case out of control?
break;
}
}
return $working_date;
}
function isWeekend($check_date) {
return (date("N", strtotime($check_date)) > 5);
}
function confirm_holiday($somedate="") {
if ($somedate=="") {
$somedate = date("Y-m-d");
}
$year = date("Y", strtotime($somedate));
$blnHoliday = false;
//newyears
if ($somedate == observed_day($year, 1, 1)) {
$blnHoliday = true;
}
if ($somedate == format_date($year, 1, 1)) {
$blnHoliday = true;
}
if ($somedate == format_date($year, 12, 31)) {
$blnHoliday = true;
}
//Martin Luther King
if ($somedate == get_holiday($year, 1, 1, 3)) {
$blnHoliday = true;
}
//President's
if ($somedate == get_holiday($year, 2, 1, 3)) {
$blnHoliday = true;
}
//easter
if ($somedate == calculate_easter($year)) {
$blnHoliday = true;
}
//Memorial
if ($somedate == get_holiday($year, 5, 1)) {
$blnHoliday = true;
}
//july4
if ($somedate == observed_day($year, 7, 4)) {
$blnHoliday = true;
}
//labor
if ($somedate == get_holiday($year, 9, 1, 1)) {
$blnHoliday = true;
}
//columbus
if ($somedate == get_holiday($year, 10, 1, 2)) {
$blnHoliday = true;
}
//thanks
if ($somedate == get_holiday($year, 11, 4, 4)) {
$blnHoliday = true;
}
//xmas
if ($somedate == format_date($year, 12, 24)) {
$blnHoliday = true;
}
if ($somedate == format_date($year, 12, 25)) {
$blnHoliday = true;
}
return $blnHoliday;
}
function get_business_days_forward_from_date($num_days, $start_date='', $rtn_fmt='Y-m-d')
{
// $start_date will default to today
if ($start_date=='') { $start_date = date("Y-m-d"); }
$business_day_ct = 0;
$max_days = 10000 + $num_days; // to avoid any possibility of an infinite loop
// define holidays, this currently only goes to 2012 because, well, you know... ;-)
// if the world is still here after that, you can find more at
// http://www.opm.gov/Operating_Status_Schedules/fedhol/2013.asp
// always add holidays in order, because the iteration will stop when the holiday is > date being tested
$fed_holidays=array(
"2010-01-01",
"2010-01-18",
"2010-02-15",
"2010-05-31",
"2010-07-05",
"2010-09-06",
"2010-10-11",
"2010-11-11",
"2010-11-25",
"2010-12-24",
"2010-12-31",
"2011-01-17",
"2011-02-21",
"2011-05-30",
"2011-07-04",
"2011-09-05",
"2011-10-10",
"2011-11-11",
"2011-11-24",
"2011-12-26",
"2012-01-02",
"2012-01-16",
"2012-02-20",
"2012-05-28",
"2012-07-04",
"2012-09-03",
"2012-10-08",
"2012-11-12",
"2012-11-22",
"2012-12-25",
);
$curr_date_ymd = date('Y-m-d', strtotime($start_date));
for ($x=1;$x<$max_days;$x++)
{
if (intval($num_days)==intval($business_day_ct)) { return(date($rtn_fmt, strtotime($curr_date_ymd))); } // date found - return
// get next day to check
$curr_date_ymd = date('Y-m-d', (strtotime($start_date)+($x * 86400))); // add 1 day to the current date
$is_business_day = 1;
// check if this is a weekend 1 (for Monday) through 7 (for Sunday)
if ( intval(date("N",strtotime($curr_date_ymd))) > 5) { $is_business_day = 0; }
//check for holiday
foreach($fed_holidays as $holiday)
{
if (strtotime($holiday)==strtotime($curr_date_ymd)) // holiday found
{
$is_business_day = 0;
break 1;
}
if (strtotime($holiday)>strtotime($curr_date_ymd)) { break 1; } // past date, stop searching (always add holidays in order)
}
$business_day_ct = $business_day_ct + $is_business_day; // increment if this is a business day
}
// if we get here, you are hosed
return ("ERROR");
}
The add_business_days has a small bug. Try the following with the existing function and the output will be a Saturday.
Startdate = Friday
Business days to add = 1
Holidays array = Add date for the following Monday.
I have fixed that in my function below.
function add_business_days($startdate, $buisnessdays, $holidays = array(), $dateformat = 'Y-m-d'){
$i= 1;
$dayx= strtotime($startdate);
$buisnessdays= ceil($buisnessdays);
while($i < $buisnessdays)
{
$day= date('N',$dayx);
$date= date('Y-m-d',$dayx);
if($day < 6 && !in_array($date,$holidays))
$i++;
$dayx= strtotime($date.' +1 day');
}
## If the calculated day falls on a weekend or is a holiday, then add days to the next business day
$day= date('N',$dayx);
$date= date('Y-m-d',$dayx);
while($day >= 6 || in_array($date,$holidays))
{
$dayx= strtotime($date.' +1 day');
$day= date('N',$dayx);
$date= date('Y-m-d',$dayx);
}
return date($dateformat, $dayx);}
Thanks to Bobbin, mcgrailm, Tony, James Pasta and a few others who posted here. I had written my own function to add business days to a date, but modified it with some code I found here. This will handle the start date being on a weekend/holiday. This will also handle business hours. I added some comments and break up the code to make it easier to read.
<?php
function count_business_days($date, $days, $holidays) {
$date = strtotime($date);
for ($i = 1; $i <= intval($days); $i++) { //Loops each day count
//First, find the next available weekday because this might be a weekend/holiday
while (date('N', $date) >= 6 || in_array(date('Y-m-d', $date), $holidays)){
$date = strtotime(date('Y-m-d',$date).' +1 day');
}
//Now that we know we have a business day, add 1 day to it
$date = strtotime(date('Y-m-d',$date).' +1 day');
//If this day that was previously added falls on a weekend/holiday, then find the next business day
while (date('N', $date) >= 6 || in_array(date('Y-m-d', $date), $holidays)){
$date = strtotime(date('Y-m-d',$date).' +1 day');
}
}
return date('Y-m-d', $date);
}
//Also add in the code from Tony and James Pasta to handle holidays...
function getNationalAmericanHolidays($year) {
$bankHolidays = array(
'New Years Day' => $year . "-01-01",
'Martin Luther King Jr Birthday' => "". date("Y-m-d",strtotime("third Monday of January " . $year) ),
'Washingtons Birthday' => "". date("Y-m-d",strtotime("third Monday of February " . $year) ),
'Memorial Day' => "". date("Y-m-d",strtotime("last Monday of May " . $year) ),
'Independance Day' => $year . "-07-04",
'Labor Day' => "". date("Y-m-d",strtotime("first Monday of September " . $year) ),
'Columbus Day' => "". date("Y-m-d",strtotime("second Monday of October " . $year) ),
'Veterans Day' => $year . "-11-11",
'Thanksgiving Day' => "". date("Y-m-d",strtotime("fourth Thursday of November " . $year) ),
'Christmas Day' => $year . "-12-25"
);
return $bankHolidays;
}
//Now to call it... since we're working with business days, we should
//also be working with business hours so check if it's after 5 PM
//and go to the next day if necessary.
//Go to next day if after 5 pm (5 pm = 17)
if (date(G) >= 17) {
$start_date = date("Y-m-d", strtotime("+ 1 day")); //Tomorrow
} else {
$start_date = date("Y-m-d"); //Today
}
//Get the holidays for the current year and also for the next year
$this_year = getNationalAmericanHolidays(date('Y'));
$next_year = getNationalAmericanHolidays(date('Y', strtotime("+12 months")));
$holidays = array_merge($this_year, $next_year);
//The number of days to count
$days_count = 10;
echo count_business_days($start_date, $days_count, $holidays);
?>
Personally, I think this is a cleaner and more concise solution:
function onlyWorkDays( $d ) {
$holidays = array('2013-12-25','2013-12-31','2014-01-01','2014-01-20','2014-02-17','2014-05-26','2014-07-04','2014-09-01','2014-10-13','2014-11-11','2014-11-27','2014-12-25','2014-12-31');
while (in_array($d->format("Y-m-d"), $holidays)) { // HOLIDAYS
$d->sub(new DateInterval("P1D"));
}
if ($d->format("w") == 6) { // SATURDAY
$d->sub(new DateInterval("P1D"));
}
if ($d->format("w") == 0) { // SUNDAY
$d->sub(new DateInterval("P2D"));
}
return $d;
}
Just send the proposed new date to this function.
I just created this function, which seems to work very well:
function getBusinessDays($date1, $date2){
if(!is_numeric($date1)){
$date1 = strtotime($date1);
}
if(!is_numeric($date2)){
$date2 = strtotime($date2);
}
if($date2 < $date1){
$temp_date = $date1;
$date1 = $date2;
$date2 = $temp_date;
unset($temp_date);
}
$diff = $date2 - $date1;
$days_diff = intval($diff / (3600 * 24));
$current_day_of_week = intval(date("N", $date1));
$business_days = 0;
for($i = 1; $i <= $days_diff; $i++){
if(!in_array($current_day_of_week, array("Sunday" => 1, "Saturday" => 7))){
$business_days++;
}
$current_day_of_week++;
if($current_day_of_week > 7){
$current_day_of_week = 1;
}
}
return $business_days;
}
echo "Business days: " . getBusinessDays("8/15/2014", "8/8/2014");
If you need to get how much time was between two dates you can use https://github.com/maximnara/business-days-counter.
It's works simply but only with laravel now $diffInSeconds = $this->datesCounter->getDifferenceInSeconds(Carbon::create(2019, 1, 1), Carbon::now(), DateCounter::COUNTRY_FR);
It doesn't counts public holidays and weekends and you can set working interval, for example from 9 to 18 with launch hour or no.
Or if you need just weekends and you use Carbon you can use built in function:
$date1 = Carbon::create(2019, 1, 1)->endOfDay();
$date2 = $dt->copy()->startOfDay();
$diff = $date1->diffFiltered(CarbonInterval::minute(), function(Carbon $date) {
return !$date->isWeekend();
}, $date2, true);
But it will foreach every minute in interval, for bit intervals it can take a while.
Although this question has over 35 answers at the time of writing, there is a better solution to this problem.
#flamingLogos provided an answer which is based on Mathematics and doesn't contain any loops. That solution provides a time complexity of Θ(1). However, the math behind it is pretty complex especially the leap year handling.
#Glavić provided a good solution that is minimal and elegant. But it doesn't preform the calculation in a constant time, so it could produce a denial of service (DOS) attack or at least timeout if used with large periods like 10 or 100 of years since it loops on 1 day interval.
So I propose a mathematical approach that have constant time and yet very readable.
The idea is to count how many days to have complete weeks.
<?php
function getWorkingHours($start_date, $end_date) {
// validate input
if(!validateDate($start_date) || !validateDate($end_date)) return ['error' => 'Invalid Date'];
if($end_date < $start_date) return ['error' => 'End date must be greater than or equal Start date'];
//We save timezone and switch to UTC to prevent issues
$old_timezone = date_default_timezone_get();
date_default_timezone_set("UTC");
$startDate = strtotime($start_date);
$endDate = strtotime($end_date);
//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 include both dates in the interval.
$days = ($endDate - $startDate) / 86400 + 1;
$no_full_weeks = ceil($days / 7);
//we get only missing days count to complete full weeks
//we take modulo 7 in case it was already full weeks
$no_of_missing_days = (7 - ($days % 7)) % 7;
$workingDays = $no_full_weeks * 5;
//Next we remove the working days we added, this loop will have max of 6 iterations.
for ($i = 1; $i <= $no_of_missing_days; $i++){
if(date('N', $endDate + $i * 86400) < 6) $workingDays--;
}
$holidays = getHolidays(date('Y', $startDate), date('Y', $endDate));
//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--;
}
date_default_timezone_set($old_timezone);
return ['days' => $workingDays];
}
The input to the function are in the format of Y-m-d in php or yyyy-mm-dd in general date format.
The get holiday function will return an array of holiday dates starting from start year and until end year.
Related
Let's say that I have two dates:
$initialDate = '08/10/2015 09:30:24 am';
$finalDate = '15/10/2015 15:47:38 pm';
$holiday = '12/10/2015';
I have to consider the hour of these days.
Hours to consider : 8 hours per day;
Start : 8 pm
End: 18 pm (24 hours format )
Lunch break start: 12:00 pm
Lunch break end: 14:00 pm
Example 1 : From 08/10/2015 10:00:00 to 09/10/2015 17:00:00 results 13 working hours. ( excludes lunch break )
Example 2 : From 08/10/2015 14:00:00 to 09/10/2015 18:00:00 results 12 working hours. ( Do not exclude 2 hours from begin date, because starts after 14:00 pm, lunch break )
Example 3 : From 08/10/2015 16:00:00 to 09/10/2015 18:00:00 results 10 working hours. ( Do not exclude 2 hours from begin date, because starts after 14:00 pmm lunch break )
Exampld 4 : From 08/10/2015 08:00:00 to 09/10/2015 11:00:00 results 14 working hours. ( Exclude 2 hours from begin date, and do not exclude 2 hours from end date, because isn't after 14:00 pm )
And I have to calculate the working hours and working days between those two dates, excluding weekends and Holidays, how can I do that ? I'm using PHP.
PS: I Already have something, but without lunch break... I made a research here on StackOverFlow.
Code:
function get_workdays($dataInicial,$dataFinal){
// arrays
$days_array = array();
$skipdays = array("Saturday", "Sunday");
$skipdates = get_feriados();
// other variables
$i = 0;
$current = $dataInicial;
if($current == $dataFinal) // same dates
{
$timestamp = strtotime($dataInicial);
if (!in_array(date("l", $timestamp), $skipdays)&&!in_array(date("Y-m-d", $timestamp), $skipdates)) {
$days_array[] = date("Y-m-d",$timestamp);
}
}
elseif($current < $dataFinal) // different dates
{
while ($current < $dataFinal) {
$timestamp = strtotime($dataInicial." +".$i." day");
if (!in_array(date("l", $timestamp), $skipdays)&&!in_array(date("Y-m-d", $timestamp), $skipdates)) {
$days_array[] = date("Y-m-d",$timestamp);
}
$current = date("Y-m-d",$timestamp);
$i++;
}
}
return $days_array;
}
function get_feriados(){
$dateAno = Date('Y');
$days_array = array(
$dateAno.'-10-12', // Padroeira do Brasil/ Dias das Crianças
$dateAno.'-11-02', // Finados
$dateAno.'-12-25' // Finados
);
return $days_array;
}
date_default_timezone_set('America/Sao_Paulo');
$dateAno = Date('Y');
$dataInicial = Date('08/10/2015 H:i');
$dataFinal = Date('13/10/2015 H:i');
// timestamps
$from_timestamp = strtotime(str_replace('/', '-', $dataInicial));
$to_timestamp = strtotime(str_replace('/', '-', $dataFinal));
// work day seconds
$workday_start_hour = 9;
$workday_end_hour = 17;
$workday_seconds = ($workday_end_hour - $workday_start_hour)*3600;
// work days beetwen dates, minus 1 day
$from_date = date('Y-m-d',$from_timestamp);
$to_date = date('Y-m-d',$to_timestamp);
$workdays_number = count(get_workdays($from_date,$to_date))-1;
$workdays_number = $workdays_number<0 ? 0 : $workdays_number;
// start and end time
$start_time_in_seconds = date("H",$from_timestamp)*3600+date("i",$from_timestamp)*60;
$end_time_in_seconds = date("H",$to_timestamp)*3600+date("i",$to_timestamp)*60;
// final calculations
$working_hours = ($workdays_number * $workday_seconds + $end_time_in_seconds - $start_time_in_seconds) / 86400 * 24;
print_r('<br/> Horas úteis '.$working_hours);
}
But don't consider two hours of break lunch. Can somebody please help me ?
If you use PHP 5.3 or higher, you can do this:
$datefrom = DateTime::createFromFormat('d/m/Y', '08/10/2015');
$dateto = DateTime::createFromFormat('d/m/Y', '15/10/2015');
$interval = $datefrom->diff($dateto);
$days = intval($interval->format('%a'));
Also you can remove holidays with if:
if ($datetime1->getTimestamp() < $holiday->getTimestamp() and $datetime2->getTimestamp() > $holiday->getTimestamp()) $days--;
Calculate hours between two days:
$datefrom = DateTime::createFromFormat('d/m/Y H:i:s', '08/10/2015 12:51:34');
$dateto = DateTime::createFromFormat('d/m/Y H:i:s', '15/10/2015 13:14:56');
$hours = intval($interval->format('%a')) * 24 + $interval->format('%h');
You can calculate hours of launches sum and then subtract it.
How to ignore weekends or calculate ignore days:
while($dateto->getTimestamp() > $datefrom->getTimestamp()) {
if (in_array($datefrom->format('w'), array('0','6'))) $ignore_days += 1;
$datefrom->modify('+1 day');
}
I expect this will do all you want. But I changed the datetime format as follows. Check it. Used less comments. If any query, please ask. Holidays are arrays, add and remove as required.
Times between 12:00 - 14:00 is handled.
Times below 08:00 is handled.
Times above 18:00 is handled.
<?php
$initialDate = '2015-10-13 08:15:00'; //start date and time in YMD format
$finalDate = '2015-10-14 11:00:00'; //end date and time in YMD format
$holiday = array('2015-10-12'); //holidays as array
$noofholiday = sizeof($holiday); //no of total holidays
//create all required date time objects
$firstdate = DateTime::createFromFormat('Y-m-d H:i:s',$initialDate);
$lastdate = DateTime::createFromFormat('Y-m-d H:i:s',$finalDate);
if($lastdate > $firstdate)
{
$first = $firstdate->format('Y-m-d');
$first = DateTime::createFromFormat('Y-m-d H:i:s',$first." 00:00:00" );
$last = $lastdate->format('Y-m-d');
$last = DateTime::createFromFormat('Y-m-d H:i:s',$last." 23:59:59" );
$workhours = 0; //working hours
for ($i = $first;$i<=$last;$i->modify('+1 day') )
{
$holiday = false;
for($k=0;$k<$noofholiday;$k++) //excluding holidays
{
if($i == $holiday[$k])
{
$holiday = true;
break;
} }
$day = $i->format('l');
if($day === 'Saturday' || $day === 'Sunday') //excluding saturday, sunday
$holiday = true;
if(!$holiday)
{
$ii = $i ->format('Y-m-d');
$f = $firstdate->format('Y-m-d');
$l = $lastdate->format('Y-m-d');
if($l ==$f )
$workhours +=sameday($firstdate,$lastdate);
else if( $ii===$f)
$workhours +=firstday($firstdate);
else if ($l ===$ii)
$workhours +=lastday($lastdate);
else
$workhours +=8;
}
}
echo $workhours; //echo the hours
}
else
echo "lastdate less than first date";
function sameday($firstdate,$lastdate)
{
$fmin = $firstdate->format('i');
$fhour = $firstdate->format('H');
$lmin = $lastdate->format('i');
$lhour = $lastdate->format('H');
if($fhour >=12 && $fhour <14)
$fhour = 14;
if($fhour <8)
$fhour =8;
if($fhour >=18)
$fhour =18;
if($lhour<8)
$lhour=8;
if($lhour>=12 && $lhour<14)
$lhour = 14;
if($lhour>=18)
$lhour = 18;
if($lmin == 0)
$min = ((60-$fmin)/60)-1;
else
$min = ($lmin-$fmin)/60;
return $lhour-$fhour + $min;
}
function firstday($firstdate) //calculation of hours of first day
{
$stmin = $firstdate->format('i');
$sthour = $firstdate->format('H');
if($sthour<8) //time before morning 8
$lochour = 8;
else if($sthour>18)
$lochour = 0;
else if($sthour >=12 && $sthour<14)
$lochour = 4;
else
{
$lochour = 18-$sthour;
if($sthour<=14)
$lochour-=2;
if($stmin == 0)
$locmin =0;
else
$locmin = 1-( (60-$stmin)/60); //in hours
$lochour -= $locmin;
}
return $lochour;
}
function lastday($lastdate) //calculation of hours of last day
{
$stmin = $lastdate->format('i');
$sthour = $lastdate->format('H');
if($sthour>=18) //time after 18
$lochour = 8;
else if($sthour<8) //time before morning 8
$lochour = 0;
else if($sthour >=12 && $sthour<14)
$lochour = 4;
else
{
$lochour = $sthour - 8;
$locmin = $stmin/60; //in hours
if($sthour>14)
$lochour-=2;
$lochour += $locmin;
}
return $lochour;
}
?>
Check the bellow code, that will return the number of Working days
function number_of_working_days($from, $to) {
$workingDays = [1, 2, 3, 4, 5];// date format = (1 = Monday,2 = Tue, ...)
$holidayDays = ['*-12-25', '*-02-14', '2015-12-23']; // variable and fixed holidays
$from = new DateTime($from);
$to = new DateTime($to);
$to->modify('+1 day');
$interval = new DateInterval('P1D');
$days = new DatePeriod($from, $interval, $to);
$no_of_working_days = 0;
foreach ($days as $day) {
if (!in_array($day->format('N'), $workingDays)||in_array($day->format('Y-m-d'), $holidayDays)||in_array($day->format('*-m-d'), $holidayDays)) {continue;}
$working_days++;
}
return $no_of_working_days;
}
echo number_of_working_days('2015-12-01', '2015-09-10');
From that you can easily calculate the Number of Working Hours.
I have created for you this nice class you can use. It requires the nesbot/carbon library (http://carbon.nesbot.com/) and you use it like so:
$calc = new HoursCalculator(
Carbon::createFromFormat("Y-m-d H:i", "2015-10-7 09:00"),
Carbon::createFromFormat("Y-m-d H:i", "2015-10-14 18:00"),
[
"2015-10-13"
]
);
echo $calc->getHours();
Heres the class:
class HoursCalculator {
const LUNCH_HOURS = 2;
protected $start;
protected $end;
protected $holidays;
protected $hoursTotal;
public function __construct(Carbon $start, Carbon $end, $holidays = [])
{
$this->start = $start;
$this->end = $end;
$this->holidays = $holidays;
}
public function getHours()
{
$dayHours = $this->getHoursInADay();
return $this->calculateHours($dayHours);
}
protected function getHoursInADay()
{
$start = $this->start;
$end = Carbon::createFromFormat("Y-m-d H:i", $this->start->format("Y-m-d") . " " . $this->end->format("H:i"));
return $start->diffInHours($end) - self::LUNCH_HOURS;
}
protected function getStartDate()
{
return $this->start->format('Y-m-d');
}
protected function calculateHours($hoursInDay)
{
$start = $this->start->copy()->startOfDay();
$end = $this->end->copy()->endOfDay();
$days = 0;
while($start->lt($end)) {
if (!$this->isHoliday($start) && !$this->isWeekend($start)) {
$days++;
}
$start->addDay(1);
}
return $days * $hoursInDay;
}
protected function isHoliday(Carbon $date)
{
$date->startOfDay();
foreach($this->holidays as $holiday) {
$holiday = Carbon::createFromFormat("Y-m-d", $holiday)->startOfDay();
if ($date->eq($holiday)) {
return true;
}
}
return false;
}
protected function isWeekend(Carbon $date)
{
return $date->isWeekend();
}
}
Hope this helps!
I have an array of random dates (not coming from MySQL). I need to group them by the week as Week1, Week2, and so on upto Week5.
What I have is this:
$dates = array('2015-09-01','2015-09-05','2015-09-06','2015-09-15','2015-09-17');
What I need is a function to get the week number of the month by providing the date.
I know that I can get the weeknumber by doing
date('W',strtotime('2015-09-01'));
but this week number is the number between year (1-52) but I need the week number of the month only, e.g. in Sep 2015 there are 5 weeks:
Week1 = 1st to 5th
Week2 = 6th to 12th
Week3 = 13th to 19th
Week4 = 20th to 26th
Week5 = 27th to 30th
I should be able to get the week Week1 by just providing the date
e.g.
$weekNumber = getWeekNumber('2015-09-01') //output 1;
$weekNumber = getWeekNumber('2015-09-17') //output 3;
I think this relationship should be true and come in handy:
Week of the month = Week of the year - Week of the year of first day of month + 1
We also need to make sure that "overlapping" weeks from the previous year are handeled correctly - if January 1st is in week 52 or 53, it should be counted as week 0. In a similar fashion, if a day in December is in the first week of the next year, it should be counted as 53. (Previous versions of this answer failed to do this properly.)
<?php
function weekOfMonth($date) {
//Get the first day of the month.
$firstOfMonth = strtotime(date("Y-m-01", $date));
//Apply above formula.
return weekOfYear($date) - weekOfYear($firstOfMonth) + 1;
}
function weekOfYear($date) {
$weekOfYear = intval(date("W", $date));
if (date('n', $date) == "1" && $weekOfYear > 51) {
// It's the last week of the previos year.
return 0;
}
else if (date('n', $date) == "12" && $weekOfYear == 1) {
// It's the first week of the next year.
return 53;
}
else {
// It's a "normal" week.
return $weekOfYear;
}
}
// A few test cases.
echo weekOfMonth(strtotime("2020-04-12")) . " "; // 2
echo weekOfMonth(strtotime("2020-12-31")) . " "; // 5
echo weekOfMonth(strtotime("2020-01-02")) . " "; // 1
echo weekOfMonth(strtotime("2021-01-28")) . " "; // 5
echo weekOfMonth(strtotime("2018-12-31")) . " "; // 6
To get weeks that starts with sunday, simply replace date("W", ...) with strftime("%U", ...).
You can use the function below, fully commented:
/**
* Returns the number of week in a month for the specified date.
*
* #param string $date
* #return int
*/
function weekOfMonth($date) {
// estract date parts
list($y, $m, $d) = explode('-', date('Y-m-d', strtotime($date)));
// current week, min 1
$w = 1;
// for each day since the start of the month
for ($i = 1; $i < $d; ++$i) {
// if that day was a sunday and is not the first day of month
if ($i > 1 && date('w', strtotime("$y-$m-$i")) == 0) {
// increment current week
++$w;
}
}
// now return
return $w;
}
The corect way is
function weekOfMonth($date) {
$firstOfMonth = date("Y-m-01", strtotime($date));
return intval(date("W", strtotime($date))) - intval(date("W", strtotime($firstOfMonth)));
}
I have created this function on my own, which seems to work correctly. In case somebody else have a better way of doing this, please share.. Here is what I have done.
function weekOfMonth($qDate) {
$dt = strtotime($qDate);
$day = date('j',$dt);
$month = date('m',$dt);
$year = date('Y',$dt);
$totalDays = date('t',$dt);
$weekCnt = 1;
$retWeek = 0;
for($i=1;$i<=$totalDays;$i++) {
$curDay = date("N", mktime(0,0,0,$month,$i,$year));
if($curDay==7) {
if($i==$day) {
$retWeek = $weekCnt+1;
}
$weekCnt++;
} else {
if($i==$day) {
$retWeek = $weekCnt;
}
}
}
return $retWeek;
}
echo weekOfMonth('2015-09-08') // gives me 2;
function getWeekOfMonth(DateTime $date) {
$firstDayOfMonth = new DateTime($date->format('Y-m-1'));
return ceil(($firstDayOfMonth->format('N') + $date->format('j') - 1) / 7);
}
Goendg solution does not work for 2016-10-31.
function weekOfMonth($strDate) {
$dateArray = explode("-", $strDate);
$date = new DateTime();
$date->setDate($dateArray[0], $dateArray[1], $dateArray[2]);
return floor((date_format($date, 'j') - 1) / 7) + 1;
}
weekOfMonth ('2015-09-17') // returns 3
Given the time_t wday (0=Sunday through 6=Saturday) of the first of the month in firstWday, this returns the (Sunday-based) week number within the month:
weekOfMonth = floor((dayOfMonth + firstWday - 1)/7) + 1
Translated into PHP:
function weekOfMonth($dateString) {
list($year, $month, $mday) = explode("-", $dateString);
$firstWday = date("w",strtotime("$year-$month-1"));
return floor(($mday + $firstWday - 1)/7) + 1;
}
You can also use this simple formula for finding week of the month
$currentWeek = ceil((date("d",strtotime($today_date)) - date("w",strtotime($today_date)) - 1) / 7) + 1;
ALGORITHM :
Date = '2018-08-08' => Y-m-d
Find out day of the month eg. 08
Find out Numeric representation of the day of the week minus 1 (number of days in week) eg. (3-1)
Take difference and store in result
Subtract 1 from result
Divide it by 7 to result and ceil the value of result
Add 1 to result eg. ceil(( 08 - 3 ) - 1 ) / 7) + 1 = 2
My function. The main idea: we would count amount of weeks passed from the month's first date to current. And the current week number would be the next one. Works on rule: "Week starts from monday" (for sunday-based type we need to transform the increasing algorithm)
function GetWeekNumberOfMonth ($date){
echo $date -> format('d.m.Y');
//define current year, month and day in numeric
$_year = $date -> format('Y');
$_month = $date -> format('n');
$_day = $date -> format('j');
$_week = 0; //count of weeks passed
for ($i = 1; $i < $_day; $i++){
echo "\n\n-->";
$_newDate = mktime(0,0,1, $_month, $i, $_year);
echo "\n";
echo date("d.m.Y", $_newDate);
echo "-->";
echo date("N", $_newDate);
//on sunday increasing weeks passed count
if (date("N", $_newDate) == 7){
echo "New week";
$_week += 1;
}
}
return $_week + 1; // as we are counting only passed weeks the current one would be on one higher
}
$date = new DateTime("2019-04-08");
echo "\n\nResult: ". GetWeekNumberOfMonth($date);
$month = 6;
$year = 2021;
$week = date("W", strtotime($year . "-" . $month ."-01"));
$str='';
$str .= date("d-m-Y", strtotime($year . "-" . $month ."-01")) ."to";
$unix = strtotime($year."W".$week ."+1 week");
while(date("m", $unix) == $month){
$str .= date("d-m-Y", $unix-86400) . "|";
$str .= date("d-m-Y", $unix) ."to";
$unix = $unix + (86400*7);
}
$str .= date("d-m-Y", strtotime("last day of ".$year . "-" . $month));
$weeks_ar = explode('|',$str);
echo '<pre>'; print_r($weeks_ar);
working fine.
// Current week of the month starts with Sunday
$first_day_of_the_week = 'Sunday';
$start_of_the_week1 = strtotime("Last $first_day_of_the_week");
if (strtolower(date('l')) === strtolower($first_day_of_the_week)) {
$start_of_the_week1 = strtotime('today');
}
$end_of_the_week1 = $start_of_the_week1 + (60 * 60 * 24 * 7) - 1;
// Get the date format
print date('Y-m-d', $start_of_the_week1) . ' 00:00:00';
print date('Y-m-d', $end_of_the_week1) . ' 23:59:59';
// self::DAYS_IN_WEEK = 7;
function getWeeksNumberOfMonth(): int
{
$currentDate = new \DateTime();
$dayNumberInMonth = (int) $currentDate->format('j');
$dayNumberInWeek = (int) $currentDate->format('N');
$dayNumberToLastSunday = $dayNumberInMonth - $dayNumberInWeek;
$daysCountInFirstWeek = $dayNumberToLastSunday % self::DAYS_IN_WEEK;
$weeksCountToLastSunday = ($dayNumberToLastSunday - $daysCountInFirstWeek) / self::DAYS_IN_WEEK;
$weeks = [];
array_push($weeks, $daysCountInFirstWeek);
for ($i = 0; $i < $weeksCountToLastSunday; $i++) {
array_push($weeks, self::DAYS_IN_WEEK);
}
array_push($weeks, $dayNumberInWeek);
if (array_sum($weeks) !== $dayNumberInMonth) {
throw new Exception('Logic is not valid');
}
return count($weeks);
}
Short variant:
(int) (new \DateTime())->format('W') - (int) (new \DateTime('first day of this month'))->format('W') + 1;
There is a many solutions but here is one my solution that working well in the most cases.
function current_week ($date = NULL) {
if($date) {
if(is_numeric($date) && ctype_digit($date) && strtotime(date('Y-m-d H:i:s',$date)) === (int)$date)
$unix_timestamp = $date;
else
$unix_timestamp = strtotime($date);
} else $unix_timestamp = time();
return (ceil((date('d', $unix_timestamp) - date('w', $unix_timestamp) - 1) / 7) + 1);
}
It accept unix timestamp, normal date or return current week from the time() if you not pass any value.
Enjoy!
I know this an old post but i have an idea!
$datetime0 = date_create("1970-01-01");
$datetime1 = date_create(date("Y-m-d",mktime(0,0,0,$m,"01",$Y)));
$datetime2 = date_create(date("Y-m-d",mktime(0,0,0,$m,$d,$Y)));
$interval1 = date_diff($datetime0, $datetime1);
$daysdiff1= $interval1->format('%a');
$interval2 = date_diff($datetime0, $datetime2);
$daysdiff2= $interval2->format('%a');
$week1=round($daysdiff1/7);
$week2=round($daysdiff2/7);
$WeekOfMonth=$week2-$week1+1;
$date = new DateTime('first Monday of this month');
$thisMonth = $date->format('m');
$mondays_arr = [];
// Get all the Mondays in the current month and store in array
while ($date->format('m') === $thisMonth) {
//echo $date->format('Y-m-d'), "\n";
$mondays_arr[] = $date->format('d');
$date->modify('next Monday');
}
// Get the day of the week (1-7 from monday to sunday)
$day_of_week = date('N') - 1;
// Get the day of month (1 to 31)
$current_week_monday_date = date('j') - $day_of_week;
/*$day_of_week = date('N',mktime(0, 0, 0, 2, 11, 2020)) - 1;
$current_week_monday_date = date('j',mktime(0, 0, 0, 2, 11, 2020)) - $day_of_week;*/
$week_no = array_search($current_week_monday_date,$mondays_arr) + 1;
echo "Week No: ". $week_no;
How about this function making use of PHP's relative dates?
This function assumes the week ends on Saturday. But this can be changed easily.
function get_weekNumMonth($date) {
$CI = &get_instance();
$strtotimedate = strtotime($date);
$firstweekEnd = date('j', strtotime("FIRST SATURDAY OF " . date("F", $strtotimedate) . " " . date("Y", $strtotimedate)));
$cutoff = date('j', strtotime($date));
$weekcount = 1;
while ($cutoff > $firstweekEnd) {
$weekcount++;
$firstweekEnd += 7; // move to next week
}
return $weekcount;
}
This function returns the integer week number of the current month. Weeks always start on Monday and counting always starts with 1.
function weekOfmonth(DateTime $date)
{
$dayFirstMonday = date_create('first monday of '.$date->format('F Y'))->format('j');
return (int)(($date->format('j') - $dayFirstMonday +7)/7) + ($dayFirstMonday == 1 ? 0 : 1);
}
Example of use
echo weekOfmonth(new DateTime("2020-04-12")); //2
A test for all days from 1900-2038 with the accepted solution from #Anders as a reference:
//reference functions
//integer $date (Timestamp)
function weekOfMonthAnders($date) {
//Get the first day of the month.
$firstOfMonth = strtotime(date("Y-m-01", $date));
//Apply above formula.
return weekOfYear($date) - weekOfYear($firstOfMonth) + 1;
}
function weekOfYear($date) {
$weekOfYear = intval(date("W", $date));
if (date('n', $date) == "1" && $weekOfYear > 51) {
// It's the last week of the previos year.
return 0;
}
else if (date('n', $date) == "12" && $weekOfYear == 1) {
// It's the first week of the next year.
return 53;
}
else {
// It's a "normal" week.
return $weekOfYear;
}
}
//this function
function weekOfmonth(DateTime $date)
{
$dayFirstMonday = date_create('first monday of '.$date->format('F Y'))->format('j');
return (int)(($date->format('j') - $dayFirstMonday +7)/7) + ($dayFirstMonday == 1 ? 0 : 1);
}
$dt = date_create('1900-01-01');
$end = date_create('2038-01-02');
$countOk = 0;
$countError = 0;
for(;$dt < $end; $dt->modify('+1 Day')){
$ts = $dt->getTimestamp();
if(weekOfmonth($dt) === weekOfMonthAnders($ts)){
++$countOk;
}
else {
++$countError;
}
}
echo $countOk.' compare ok, '.$countError.' errors';
Result: 50405 compare ok, 0 errors
I took the visual approach (like how we do it in the real world). Instead of using formulas or what not, I solved it (or at least I think I did) by visualizing a literal calendar and then putting the dates in a multidimensional array. The first dimension corresponds to the week.
I hope someone can check if it stands your tests. Or help someone out with a different approach.
# date in this format 2021-08-03
# week_start is either Sunday or Monday
function getWeekOfMonth($date, $week_start = "Sunday"){
list($year, $month, $day) = explode("-", $date);
$dates = array();
$current_week = 1;
$new_week_signal = $week_start == "Sunday" ? 6 : 0;
for($i = 1; $i <= date("t", strtotime($date)); $i++){
$current_date = strtotime("{$year}-{$month}-".$i);
$dates[$current_week][] = $i;
if(date('w', $current_date) == $new_week_signal){
$current_week++;
}
}
foreach($dates as $week => $days){
if(in_array(intval($day), $days)){
return $week;
}
}
return false;
}
//It's easy, no need to use php function
//Let's say your date is 2017-07-02
$Date = explode("-","2017-07-02");
$DateNo = $Date[2];
$WeekNo = $DateNo / 7; // devide it with 7
if(is_float($WeekNo) == true)
{
$WeekNo = ceil($WeekNo); //So answer will be 1
}
//If value is not float then ,you got your answer directly
I want to count the total day difference from user input
For example when the user inputs
start_date = 2012-09-06 and end-date = 2012-09-11
For now I am using this code to find the diffeence
$count = abs(strtotime($start_date) - strtotime($end_date));
$day = $count+86400;
$total = floor($day/(60*60*24));
The result of total will be 6. But the problem is that I dont want to include the days at weekend (Saturday and Sunday)
2012-09-06
2012-09-07
2012-09-08 Saturday
2012-09-09 Sunday
2012-09-10
2012-09-11
So the result will be 4
----update---
I have a table that contains date,the table name is holiday date
for example the table contains 2012-09-07
So, the total day will be 3, because it didn't count the holiday date
how do I do that to equate the date from input to date in table?
Very easy with my favourites: DateTime, DateInterval and DatePeriod
$start = new DateTime('2012-09-06');
$end = new DateTime('2012-09-11');
// otherwise the end date is excluded (bug?)
$end->modify('+1 day');
$interval = $end->diff($start);
// total days
$days = $interval->days;
// create an iterateable period of date (P1D equates to 1 day)
$period = new DatePeriod($start, new DateInterval('P1D'), $end);
// best stored as array, so you can add more than one
$holidays = array('2012-09-07');
foreach($period as $dt) {
$curr = $dt->format('D');
// substract if Saturday or Sunday
if ($curr == 'Sat' || $curr == 'Sun') {
$days--;
}
// (optional) for the updated question
elseif (in_array($dt->format('Y-m-d'), $holidays)) {
$days--;
}
}
echo $days; // 4
In my case I needed the same answer as OP, but wanted something a little smaller. #Bojan's answer worked, but I didn't like that it doesn't work with DateTime objects, required using timestamps, and was comparing against strings instead of the actual objects themselves (which feels hacky)... Here's a revised version of his answer.
function getWeekdayDifference(\DateTime $startDate, \DateTime $endDate)
{
$days = 0;
while($startDate->diff($endDate)->days > 0) {
$days += $startDate->format('N') < 6 ? 1 : 0;
$startDate = $startDate->add(new \DateInterval("P1D"));
}
return $days;
}
Per #xzdead's comment if you'd like this to be inclusive of the start and end date:
function getWeekdayDifference(\DateTime $startDate, \DateTime $endDate)
{
$isWeekday = function (\DateTime $date) {
return $date->format('N') < 6;
};
$days = $isWeekday($endDate) ? 1 : 0;
while($startDate->diff($endDate)->days > 0) {
$days += $isWeekday($startDate) ? 1 : 0;
$startDate = $startDate->add(new \DateInterval("P1D"));
}
return $days;
}
The easiest and fastest way to get difference without weekends is by using Carbon library.
Here's an example how to use it:
<?php
$from = Carbon\Carbon::parse('2016-05-21 22:00:00');
$to = Carbon\Carbon::parse('2016-05-21 22:00:00');
echo $to->diffInWeekdays($from);
use DateTime:
$datetime1 = new DateTime('2012-09-06');
$datetime2 = new DateTime('2012-09-11');
$interval = $datetime1->diff($datetime2);
$woweekends = 0;
for($i=0; $i<=$interval->d; $i++){
$datetime1->modify('+1 day');
$weekday = $datetime1->format('w');
if($weekday !== "0" && $weekday !== "6"){ // 0 for Sunday and 6 for Saturday
$woweekends++;
}
}
echo $woweekends." days without weekend";
// 4 days without weekends
date('N') gets the day of the week (1 - Monday, 7 - Sunday)
$start = strtotime('2012-08-06');
$end = strtotime('2012-09-06');
$count = 0;
while(date('Y-m-d', $start) < date('Y-m-d', $end)){
$count += date('N', $start) < 6 ? 1 : 0;
$start = strtotime("+1 day", $start);
}
echo $count;
Here is the improved version of #dan-lee function:
function get_total_days($start, $end, $holidays = [], $weekends = ['Sat', 'Sun']){
$start = new \DateTime($start);
$end = new \DateTime($end);
$end->modify('+1 day');
$total_days = $end->diff($start)->days;
$period = new \DatePeriod($start, new \DateInterval('P1D'), $end);
foreach($period as $dt) {
if (in_array($dt->format('D'), $weekends) || in_array($dt->format('Y-m-d'), $holidays)){
$total_days--;
}
}
return $total_days;
}
To use it:
$start = '2021-06-12';
$end = '2021-06-17';
$holidays = ['2021-06-15'];
echo get_total_days($start, $end, $holidays); // Result: 3
Have a look at this post:
Calculate business days
(In your case, you could leave out the 'holidays' part since you're after working/business days only)
<?php
//The function returns the no. of business days between two dates
function getWorkingDays($startDate,$endDate){
// 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;
}
return $workingDays;
}
// This will return 4
echo getWorkingDays("2012-09-06","2012-09-11");
?>
If you don't need full days but accurate seconds instead try this code. This accepts unix timestamps as an input.
function timeDifferenceWithoutWeekends($from, $to) {
$start = new DateTime("#".$from);
$current = clone $start;
$end = new DateTime("#".$to);
$sum = 0;
while ($current<$end) {
$endSlice = clone $current;
$endSlice->setTime(0,0,0);
$endSlice->modify('+1 day');
if ($endSlice>$end) {
$endSlice= clone $end;
}
$seconds = $endSlice->getTimestamp()-$current->getTimestamp();
$currentDay = $current->format("D");
if ($currentDay != 'Sat' && $currentDay != 'Sun') {
$sum+=$seconds;
}
$current = $endSlice;
}
return $sum;
}
/**
* Getting the Weekdays count[ Excludes : Weekends]
*
* #param type $fromDateTimestamp
* #param type $toDateTimestamp
* #return int
*/
public static function getWeekDaysCount($fromDateTimestamp = null, $toDateTimestamp=null) {
$startDateString = date('Y-m-d', $fromDateTimestamp);
$timestampTomorrow = strtotime('+1 day', $toDateTimestamp);
$endDateString = date("Y-m-d", $timestampTomorrow);
$objStartDate = new \DateTime($startDateString); //intialize start date
$objEndDate = new \DateTime($endDateString); //initialize end date
$interval = new \DateInterval('P1D'); // set the interval as 1 day
$dateRange = new \DatePeriod($objStartDate, $interval, $objEndDate);
$count = 0;
foreach ($dateRange as $eachDate) {
if ( $eachDate->format("w") != 6
&& $eachDate->format("w") != 0
) {
++$count;
}
}
return $count;
}
Kindly have a look at this precise php function returning days count with weekends excluded.
function Count_Days_Without_Weekends($start, $end){
$days_diff = floor(((abs(strtotime($end) - strtotime($start))) / (60*60*24)));
$run_days=0;
for($i=0; $i<=$days_diff; $i++){
$newdays = $i-$days_diff;
$futuredate = strtotime("$newdays days");
$mydate = date("F d, Y", $futuredate);
$today = date("D", strtotime($mydate));
if(($today != "Sat") && ($today != "Sun")){
$run_days++;
}
}
return $run_days;
}
Try it out, it really works..
A very simple solution using Carbon\Caborn
here is the repository file which is called from a controller store function
<?php
namespace App\Repositories\Leave;
use App\Models\Holiday;
use App\Models\LeaveApplication;
use App\Repositories\BaseRepository;
use Carbon\Carbon;
class LeaveApplicationRepository extends BaseRepository
{
protected $holiday;
public function __construct(LeaveApplication $model, Holiday $holiday)
{
parent::__construct($model);
$this->holiday = $holiday;
}
/**
* Get all authenticated user leave
*/
public function getUserLeave($id)
{
return $this->model->where('employee_id',$id)->with(['leave_type','approver'])->get();
}
/**
* #param array $request
*/
public function create($request)
{
$request['total_days'] = $this->getTotalDays($request['start_date'],$request['end_date']);
return $this->model->create($request->only('send_to','leave_type_id','start_date','end_date','desc','total_days'));
}
/**
* Get total leave days
*/
private function getTotalDays($startDate, $endDate)
{
$holidays = $this->getHolidays(); //Get all public holidays
$leaveDays = 0; //Declare values which hold leave days
//Format the dates
$startDate = Carbon::createFromFormat('Y-m-d',$startDate);
$endEnd = Carbon::createFromFormat('Y-m-d',$endDate);
//Check user dates
for($date = $startDate; $date <= $endEnd; $date->modify('+1 day')) {
if (!$date->isWeekend() && !in_array($date,$holidays)) {
$leaveDays++; //Increment days if not weekend and public holidays
}
}
return $leaveDays; //return total days
}
/**
* Get Current Year Public Holidays
*/
private function getHolidays()
{
$holidays = array();
$dates = $this->holiday->select('date')->where('active',1)->get();
foreach ($dates as $date) {
$holidays[]=Carbon::createFromFormat('Y-m-d',$date->date);
}
return $holidays;
}
}
Controller function receives user input request and validate before call the repository function
<?php
namespace App\Http\Controllers\Leave;
use App\Http\Controllers\AuthController;
use App\Http\Requests\Leave\LeaveApplicationRequest;
use App\Repositories\Leave\LeaveApplicationRepository;
use Exception;
class LeaveApplicationController extends AuthController
{
protected $leaveApplication;
/**
* LeaveApplicationsController constructor.
*/
public function __construct(LeaveApplicationRepository $leaveApplication)
{
parent::__construct();
$this->leaveApplication = $leaveApplication;
}
/**
* Store a newly created resource in storage.
*/
public function store(LeaveApplicationRequest $request)
{
try {
$this->leaveApplication->create($request);
return $this->successRoute('leaveApplications.index','Leave Applied');
}
catch (Exception $e) {
return $this->errorWithInput($request);
}
}
}
Here's an alternative to calculate business days between two dates and also excludes USA holidays using Pear's Date_Holidays from http://pear.php.net/package/Date_Holidays.
$start_date and $end_date should be DateTime objects (you can use new DateTime('#'.$timestamp) to convert from timestamp to DateTime object).
<?php
function business_days($start_date, $end_date)
{
require_once 'Date/Holidays.php';
$dholidays = &Date_Holidays::factory('USA');
$days = 0;
$period = new DatePeriod($start_date, new DateInterval('P1D'), $end_date);
foreach($period as $dt)
{
$curr = $dt->format('D');
if($curr != 'Sat' && $curr != 'Sun' && !$dholidays->isHoliday($dt->format('Y-m-d')))
{
$days++;
}
}
return $days;
}
?>
I want to calculate the number of weekdays days in a give month and year. Weekdays means monday to friday. How do i do it ?
You don't need to count every day in the month. You already know the first 28 days contain 20 weekdays no matter what. All you have to do is determine the last few days. Change the start value to 29. Then add 20 weekdays to your return value.
function get_weekdays($m,$y) {
$lastday = date("t",mktime(0,0,0,$m,1,$y));
$weekdays=0;
for($d=29;$d<=$lastday;$d++) {
$wd = date("w",mktime(0,0,0,$m,$d,$y));
if($wd > 0 && $wd < 6) $weekdays++;
}
return $weekdays+20;
}
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.
try this one
function getWeekdays($m, $y = NULL){
$arrDtext = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
if(is_null($y) || (!is_null($y) && $y == ''))
$y = date('Y');
$d = 1;
$timestamp = mktime(0,0,0,$m,$d,$y);
$lastDate = date('t', $timestamp);
$workingDays = 0;
for($i=$d; $i<=$lastDate; $i++){
if(in_array(date('D', mktime(0,0,0,$m,$i,$y)), $arrDtext)){
$workingDays++;
}
}
return $workingDays;
}
This is the simplest code I could come up with.
You really would need to create an array or a database table to hold the holidays to get a true, "Working Days" count, but that wasn't what was asked, so here you go, hope this helps someone.
function get_weekdays($m,$y) {
$lastday = date("t",mktime(0,0,0,$m,1,$y));
$weekdays=0;
for($d=1;$d<=$lastday;$d++) {
$wd = date("w",mktime(0,0,0,$m,$d,$y));
if($wd > 0 && $wd < 6) $weekdays++;
}
return $weekdays;
}
Get the number of working days without holidays between two dates :
Use example:
echo number_of_working_days('2013-12-23', '2013-12-29');
Output:
3
Link to the function
DateObject method:
function getWorkingDays(DateTime $date) {
$month = clone $date;
$month->modify('last day of this month');
$workingDays = 0;
for ($i = $month->format('t'); $i > 28; --$i) {
if ($month->format('N') < 6) {
++$workingDays;
}
$month->modify('-1 day');
}
return 20 + $workingDays;
}
Calculate working days in a month from any date:
public function getworkd($mday)
{
$dn = new DateTime($mday);
$dfrom = $dn->format('Y-m-01');
$dtill = $dn->format('Y-m-t');
$df = new DateTime($dfrom);
$dt = new DateTime($dtill);
$wdays = 0;
while($df<=$dt)
{
$dof= $df->format('D') ;
if( $dof == 'Sun' || $dof == 'Sat' ) ; else $wdays++;
$df->add(new DateInterval('P1D'));
}
return $wdays;
}
Find the last day and the weekday for the given month
then do a simple while loop like :-
$dates = explode(',', date('t,N', strtotime('2013-11-01')));
$day = $dates[1];
$tot = $dates[0];
$cnt = 0;
while ($tot>1)
{
if ($day < 6)
{
$cnt++;
}
if ($day == 1)
{
$day = 7;
}
else
{
$day--;
}
$tot--;
}
$cnt = total of weekday (Monday to Friday) for a given month
I've come up with a non-loop function. Much better in terms of performance. It might seem messy but it just needs to ask PHP the first day's weekday and the month's number days: the rest are arithmetical operations based on logic.
function countWorkDays($year, $month)
{
$workingWeekdays = 5;
$firstDayTimestamp = mktime(0, 0, 0, $month, 1, $year);
$firstDayWeekDay = (int)date("N", $firstDayTimestamp); //1: monday, 7: saturday
$upToDay = (int)date("t", $firstDayTimestamp);
$firstMonday = 1 === $firstDayWeekDay ? 1 : 9 - $firstDayWeekDay;
$wholeWeeks = $firstMonday < $upToDay ? (int)floor(($upToDay - $firstMonday + 1) / 7) : 0;
$extraDays = ($upToDay - $firstMonday + 1) % 7;
$initialWorkdays = $firstMonday > 1 && $firstDayWeekDay <= $workingWeekdays ? $workingWeekdays - $firstDayWeekDay + 1 : 0;
$workdaysInWholeWeeks = $wholeWeeks * $workingWeekdays;
$extraWorkdays = $extraDays <= $workingWeekdays ? $extraDays : $workingWeekdays;
return $initialWorkdays + $workdaysInWholeWeeks + $extraWorkdays;
}
These functions work Without Loops.
The functions calculate the number of weekdays using:
day-number of first monday in month
number of days in month
// main functions
// weekdays in month of year
function calculateNumberOfWeekDaysAtDate($month, $year)
{
// I'm sorry, I don't know the right format for the $month and $year, I hope this is right.
// PLEASE CORRECT IF WRONG
$firstMondayInCurrentMonth = (int) date("j", strtotime("first monday of 01-$month-$year")); //get first monday in month for calculations
$numberOfDaysOfCurrentMonth = (int) date("t", strtotime("01-$month-$year")); // number of days in month
return calculateNumberOfWeekDaysFromFirstMondayAndNumberOfMonthDays($firstMondayInCurrentMonth, $numberOfDaysOfCurrentMonth);
}
// week days in current month
function calculateNumberOfWeekDaysInCurrentMonth()
{
$firstMondayInCurrentMonth = (int) date("j", strtotime("first monday of this month")); //get first monday in month for calculations
$numberOfDaysOfCurrentMonth = (int) date("t"); // number of days in this month
return calculateNumberOfWeekDaysFromFirstMondayAndNumberOfMonthDays($firstMondayInCurrentMonth, $numberOfDaysOfCurrentMonth);
}
// helper functions
function calculateNumberOfWeekDaysFromFirstMondayAndNumberOfMonthDays($firstMondayInCurrentMonth, $numberOfDaysOfCurrentMonth)
{
return $numberOfWeekDays = (($start = ($firstMondayInCurrentMonth - 3)) < 0 ? 0 : $start) + floor(($numberOfDaysOfCurrentMonth - ($firstMondayInCurrentMonth - 1)) / 7) * 5 + (($rest = (($numberOfDaysOfCurrentMonth - ($firstMondayInCurrentMonth - 1)) % 7)) <= 5 ? $rest : 5);
}
function workingDays($m,$y) {
$days = cal_days_in_month(CAL_GREGORIAN, $m, $y);
$workig_days = 0;
$days_rest = array(5,6); //friday,saturday
for ( $d=1 ; $d < $days+1 ; $d++ ) {
if ( !in_array(date("w",strtotime("{$d}-{$m}-{$y}")),$days_rest) ) {
$workig_days++;
}
}
return $workig_days;
}
I created a simple function that takes the $first_day_of_month (week day like Sunday/Monday etc). You can find out the first day of month like this:
date('N', strtotime(date("01-m-Y")));
And using the $month_last_date which can be procured like this:
date("t");
Here is the function:
function workingDaysInMonth(int $first_day_of_month, int $month_last_date) : array
{
$working_days = [];
$day = $first_day_of_month;
$working_day_count = 0;
for ($i = 1; $i <= $month_last_date; $i++) {
if ($day == 8) {
$day = 1;
}
if (!($day == 6 || $day == 7)) {
$working_day_count++;
$working_days[$i] = $working_day_count;
}
$day++;
}
return $working_days;
}
this will work
// oct. 2013
$month = 10;
// loop through month days
for ($i = 1; $i <= 31; $i++) {
// given month timestamp
$timestamp = mktime(0, 0, 0, $month, $i, 2012);
// to be sure we have not gone to the next month
if (date("n", $timestamp) == $month) {
// current day in the loop
$day = date("N", $timestamp);
// if this is between 1 to 5, weekdays, 1 = Monday, 5 = Friday
if ($day == 1 OR $day <= 5) {
// write it down now
$days[$day][] = date("j", $timestamp);
}
}
}
// to see if it works :)
print_r($days);
I have 2 shipping zones, A & B. Orders for zone A are delivered each Monday, Wednesday & Friday, whereas zone B is on Tuesday, Thursday, Saturday. For each order, the delivery day is scheduled for the NEXT AVAILABLE day, depending on the zone. Please consider that if someone places an order on Monday the goods will be delivered on the NEXT available date, that would be Tuesday for zone B and Wednesday for zone A.
BUT: If an order is placed later than 18:00 (6pm) and the customer's area is within the tomorrow's delivery zone, then we have to advance the delivery date to the next available day for that zone (because the order won't be ready yet, too short notice).
Here's the code and it works fine except for the last part where I need to check time, get the regular delivery date and compare it with tomorrow's date.
<?
$date = array(date("d"), date("m"), date("Y"));
$zones = array("A" => array(1 => "Monday",
3 => "Wednesday",
5 => "Friday")
,"B" => array(2 => "Tuesday",
4 => "Thursday",
6 => "Saturday"));
$found = false;
$days_plus = 1; // always begin from next day
// Retrieve last day from the zone
end($zones[$zone]); //Friday or Saturday
$last_day = key($zones[$zone]); //5 or 6
do {
$mk = mktime(0, 0, 0, $date[1], ($date[0] + $days_plus), $date[2]);
$week = date("w", $mk); // current day of week
if ($week <= $last_day) // if weekday not passed last day of zone
{
if (!isset($zones[$zone][$week]))
{
$days_plus++;
}
else
{
$found = true;
$day = $last_day;
}
}
else
{
$days_plus++;
}
} while (!$found);
$deliverydate = date("d/m/Y", $mk); // regular delivery date
$tomorrow = date('d/m/Y', strtotime('tomorrow'));
//Now, check if order is placed after 6pm
if ((date("d/m/Y", $mk)==$tomorrow) && (date('G') > 18)) {
// HERE'S my problem, how do I advance the
//delivery date to next day in the same zone?
}
echo $deliverydate;
?>
Note: I tried converting the loop into a function findNextDay($days_plus, $date, $last_day, $zone) so that I could call it again with a different $days_plus value (increased by x days) but I couldn't get it to work. For anyone interested here is the modified version
This is how I would do it.
NOTE
I didn't give a whole lot of time to figuring out the best way to handle all of the business rules, but all of my tests seemed to work.
<?php
error_reporting(E_ALL);
ini_set('display_errors','On');
date_default_timezone_set('America/Los_Angeles');
$zones = array('A', 'B');
$zone_key = array_rand($zones);
$zone = $zones[$zone_key];
function getDeliveryDate($zone, $d = NULL, $time = NULL) {
if ($time === NULL)
$time = date('G');
if ($d === NULL)
$now = strtotime('now');
else
$now = strtotime($d);
$d = date('w', $now);
$days = 1;
if ($zone == 'A') {
if ($d % 2) $days += 1;
else if ($time > 18) $days += 2;
if ($d + $days == 7) $days += 1;
elseif ($d + $days > 7) $days -= 1;
} else {
if (! ($d % 2)) $days += 1;
else if ($time > 18) $days += 2;
if ($d + $days > 7) $days += 1;
}
$delivery_date = strtotime("+${days}days", $now);
echo "For zone $zone, schedule on " . date('D', $now) . ", at $time, will deliver on " . date('m-d-Y', $delivery_date) . " which is a " . date('D', $delivery_date) . "\n";
}
$days = array('10/16/2011', '10/17/2011', '10/18/2011', '10/19/2011', '10/20/2011', '10/21/2011', '10/22/2011');
$times = array(17, 20);
foreach ($zones as $zone) {
foreach ($days as $d) {
foreach ($times as $t) {
getDeliveryDate($zone, $d, $t);
}
}
}