CalendarSegment::getActiveTimeBetween PHP Method

getActiveTimeBetween() static public method

Get segments of a calendar between 2 date
static public getActiveTimeBetween ( $calendars_id, $day, $begin_time, $end_time ) : timestamp
$calendars_id id of the calendar
$day day number
$begin_time begin time to check
$end_time end time to check
return timestamp value
    static function getActiveTimeBetween($calendars_id, $day, $begin_time, $end_time)
    {
        global $DB;
        $sum = 0;
        // Do not check hour if day before the end day of after the begin day
        $query = "SELECT TIMEDIFF(LEAST('{$end_time}',`end`),\n                       GREATEST(`begin`,'{$begin_time}')) AS TDIFF\n                FROM `glpi_calendarsegments`\n                WHERE `calendars_id` = '{$calendars_id}'\n                      AND `day` = '{$day}'\n                      AND (`begin` < '{$end_time}')\n                      AND ('{$begin_time}' < `end`)";
        if ($result = $DB->query($query)) {
            if ($DB->numrows($result)) {
                while ($data = $DB->fetch_assoc($result)) {
                    list($hour, $minute, $second) = explode(':', $data['TDIFF']);
                    $sum += $hour * HOUR_TIMESTAMP + $minute * MINUTE_TIMESTAMP + $second;
                }
            }
        }
        return $sum;
    }

Usage Example

 /**
  * Get days durations including all segments of the current calendar
  *
  * @return end date
  **/
 function getDaysDurations()
 {
     if (!isset($this->fields['id'])) {
         return false;
     }
     $results = array();
     for ($i = 0; $i < 7; $i++) {
         /// Before PHP 5.3 need to be 23:59:59 and not 24:00:00
         $results[$i] = CalendarSegment::getActiveTimeBetween($this->fields['id'], $i, '00:00:00', '23:59:59');
     }
     return $results;
 }
All Usage Examples Of CalendarSegment::getActiveTimeBetween