0

How to count date difference excluding weekend and holidays?

I should count the number of days between two dates but the weekends and holidays must be excluded from the counting. I used Computed Field.

$start_date_value = array_pop(field_get_items($entity_type, $entity, 'field_date_from'));

$end_date_value = array_pop(field_get_items($entity_type, $entity, 'field_date_to'));

$start_date = new DateObject($start_date_value['value'], '');

$end_date = new DateObject($end_date_value['value'], '');

$entity_field[0]['value'] =$start_date->difference($end_date, 'days');

Here is my working code for counting the days.

2 Answers 2

1

why would you try something like this,

create a variable array that contain all the Hollidays

$currentyear=the current year;
$holiday['01-01-'.$currentyear,other holiday,other holiday];

$result=$start_date->difference($end_date, 'days');
for(ctr=0;ctr=sizeof(holiday);ctr++;)
{
    if(holiday[ctr]>=start_date && holiday[ctr]<=end_date)
    temp++;

} 
$result -= temp;
2
  • Thanks @junnel. I'll try it. But the code is for holidays only. Can you also help me to exclude the weekends in the counting?
    – noob
    Commented Apr 24, 2014 at 9:48
  • by that i woudl now suggest to create first a function to count the weekdays base on your from_date to end_date befor you go to holliday exclusion
    – junnel
    Commented Apr 24, 2014 at 9:53
0

I think you'll find this answer helpful, though it's PHP only:

https://stackoverflow.com/questions/336127/calculate-business-days/19221403#19221403

So you should only need to input your $start_date/$end_date values in correct format and then also define the holidays.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.