function availability_calendar_get_period_information in Availability Calendars 7.5
Same name and namespace in other branches
- 7.4 availability_calendar.inc \availability_calendar_get_period_information()
Returns an array with information about the period defined by the parameters.
The information returned:
- from (DateTime)
- to (DateTime)
- duration (int)
- timestamp_from (int)
- timestamp_to (int)
[- to_or_duration (the original 2nd parameter)]
Parameters
DateTime $from: The start date of the period.
DateTime|int $to_or_duration: Either the last (full) day (DateTime) or the duration of the period (int).
Return value
array|false The array with information or false if 1 of the parameters is incorrect.
3 calls to availability_calendar_get_period_information()
- availability_calendar_is_available in ./
availability_calendar.inc - Checks whether a calendar is available for the given period.
- availability_calendar_query_available in ./
availability_calendar.inc - Adds a where clauses to the given query to filter on availability.
- availability_calendar_query_search_api_db_search_alter in ./
availability_calendar.inc - Implements hook_query_TAG_alter().
File
- ./
availability_calendar.inc, line 1015
Code
function availability_calendar_get_period_information($from, $to_or_duration) {
// PHP5.3 adds methods getTimestamp() and diff() and class DateInterval, so
// we can use getTimestamp() instead of (int) $date->format('U') and
// $diff = $date1->diff($date2)->days instead of
// $diff = (int) round(($timestamp2 - $timestamp1)/(60*60*24));
$info = array(
'from' => $from,
'to_or_duration' => $to_or_duration,
);
if (!$info['from'] instanceof DateTime) {
return FALSE;
}
$info['from']
->setTime(0, 0, 0);
$info['timestamp_from'] = (int) $info['from']
->format('U');
if ($info['to_or_duration'] instanceof DateTime) {
$info['to'] = $info['to_or_duration'];
$info['to']
->setTime(0, 0, 0);
$info['timestamp_to'] = (int) $info['to']
->format('U');
$diff = (int) round(($info['timestamp_to'] - $info['timestamp_from']) / (60 * 60 * 24));
$info['duration'] = $diff + 1;
}
else {
$info['duration'] = (int) $info['to_or_duration'];
$diff = $info['duration'] - 1;
$info['to'] = clone $info['from'];
$info['to']
->modify("+{$diff} days");
$info['to']
->setTime(0, 0, 0);
$info['timestamp_to'] = (int) $info['to']
->format('U');
}
// Check parameters.
if ($info['duration'] <= 0) {
return FALSE;
}
return $info;
}