⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Count days between dates without friday or saturday or etc



tofayelahmed
  • profile picture
  • Member

Posted 07 October 2012 - 12:44 PM

Hi all.
I have two dates.
But I wants to need to calculate the number of days between dates without friaday or any other day,

tofayelahmed
  • profile picture
  • Member

Posted 08 October 2012 - 05:49 AM

Hi all. The problem is solved.


// caculate number of days between dates
$days = $this->get_days($start_date, $end_date);

// calculate number of weekends
$weekend = "Fri";
$weekend_days = $this->get_weekend_days($weekend,$days,$start_date);

$actual_working_days = $days - $weekend_days

function get_days($from, $to)
{
$first_date = strtotime($from);
$second_date = strtotime($to);
$offset = $second_date-$first_date;
return floor($offset/60/60/24);
}

function get_weekend_days($weekend,$days,$start_date)
{
$no_weekends = 0;
for($i=0;$i<$days + 1;$i++)
{
$date = strtotime(date("Y-m-d", strtotime($start_date)) . " +$i day");

$new_date = date("D",$date);

if($new_date == $weekend)
{
$no_weekends = $no_weekends +1;
}
}
return $no_weekends;
}