Calendar::getActiveTimeBetween PHP Method

getActiveTimeBetween() public method

Get active time between to date time for the active calendar
public getActiveTimeBetween ( $start, $end, $work_in_days = false ) : timestamp
$start datetime begin
$end datetime end
$work_in_days boolean force working in days (false by default)
return timestamp of delay
    function getActiveTimeBetween($start, $end, $work_in_days = false)
    {
        if (!isset($this->fields['id'])) {
            return false;
        }
        if ($end < $start) {
            return 0;
        }
        $timestart = strtotime($start);
        $timeend = strtotime($end);
        $datestart = date('Y-m-d', $timestart);
        $dateend = date('Y-m-d', $timeend);
        // Need to finish at the closing day : set hour to midnight :
        /// Before PHP 5.3 need to be 23:59:59 and not 24:00:00
        $timerealend = strtotime($dateend . ' 23:59:59');
        $activetime = 0;
        if ($work_in_days) {
            $activetime = $timeend - $timestart;
        } else {
            $cache_duration = $this->getDurationsCache();
            for ($actualtime = $timestart; $actualtime <= $timerealend; $actualtime += DAY_TIMESTAMP) {
                $actualdate = date('Y-m-d', $actualtime);
                if (!$this->isHoliday($actualdate)) {
                    $beginhour = '00:00:00';
                    /// Before PHP 5.3 need to be 23:59:59 and not 24:00:00
                    $endhour = '23:59:59';
                    $dayofweek = self::getDayNumberInWeek($actualtime);
                    $timeoftheday = 0;
                    if ($actualdate == $datestart) {
                        // First day : cannot use cache
                        $beginhour = date('H:i:s', $timestart);
                    }
                    if ($actualdate == $dateend) {
                        // Last day : cannot use cache
                        $endhour = date('H:i:s', $timeend);
                    }
                    if (($actualdate == $datestart || $actualdate == $dateend) && $cache_duration[$dayofweek] > 0) {
                        $timeoftheday = CalendarSegment::getActiveTimeBetween($this->fields['id'], $dayofweek, $beginhour, $endhour);
                    } else {
                        $timeoftheday = $cache_duration[$dayofweek];
                    }
                    // echo "time of the day = $timeoftheday ".Html::timestampToString($timeoftheday).'<br>';
                    $activetime += $timeoftheday;
                    // echo "cumulate time = $activetime ".Html::timestampToString($activetime).'<br>';
                } else {
                    // echo "$actualdate is an holiday<br>";
                }
            }
        }
        return $activetime;
    }

Usage Example

 /**
  * Compute close delay stat of the current ticket
  **/
 function computeCloseDelayStat()
 {
     if (isset($this->fields['id']) && !empty($this->fields['date']) && !empty($this->fields['closedate'])) {
         $calendars_id = Entity::getUsedConfig('calendars_id', $this->fields['entities_id']);
         $calendar = new Calendar();
         // Using calendar
         if ($calendars_id > 0 && $calendar->getFromDB($calendars_id)) {
             return max(0, $calendar->getActiveTimeBetween($this->fields['date'], $this->fields['closedate']) - $this->fields["waiting_duration"]);
         }
         // Not calendar defined
         return max(0, strtotime($this->fields['closedate']) - strtotime($this->fields['date']) - $this->fields["waiting_duration"]);
     }
     return 0;
 }
All Usage Examples Of Calendar::getActiveTimeBetween