I'm trying to calculate number of days between few dates in my entity Symfony2. I have the entity with getDateFrom() and getDateTo(), which return DateTime. I write function :
public function getCountDays()
{
$total = array();
foreach($this->getHolidays() as $day)
{
$total[] = (new DateTime($day->getDateTo()->format('Y-m-d')))->diff(new DateTime($day->getDateFrom()->format('Y-m-d')))->format('%a')+1 ;
$sum = array_sum($total);
//print_r( $sum ) ;
}
return $sum;
}
Everything works, but this is not what I need. Function returns result "3567" (between the first dates: period is 3 days, between second dates: period is 2 days, and between third dates: period is 1 day and between fourth dates period is 1 day). Calculation is good, but I need only return 7. Any Ideas?
I found the solution, the problem was easy to solve. Array_sum must be after foreach loop like below.
public function getCountDays()
{
$total = array();
foreach($this->getHolidays() as $day)
{
$total[] = (new DateTime($day->getDateTo()->format('Y-m-d')))->diff(new DateTime($day->getDateFrom()->format('Y-m-d')))->format('%a')+1 ;
}
$sum = array_sum($total);
return $sum;
}
Thanx for help.