Skip to content

Instantly share code, notes, and snippets.

@cappadona
Created September 26, 2014 04:43
Show Gist options
  • Save cappadona/06dad91dfac93544f7cd to your computer and use it in GitHub Desktop.
Save cappadona/06dad91dfac93544f7cd to your computer and use it in GitHub Desktop.
Using the CUL hours web service

Library Hours Service

The hours service can be queried to retrieve hours for each unit library within CUL. Some of the libraries even manage separate hours for service desks or rooms within the building.

Example calls are documented on the site and include a list of all available locations with hours data.

<?php
date_default_timezone_set('America/New_York');
$host = 'http://mannservices.mannlib.cornell.edu/LibServices';
$now = time();
// uncomment $now for testing system with different datetime
// $now = strtotime('Sep. 13, 2012 8am');
$timeFormat = 'g:i a';
$timeAmPm = 'a';
$timeCompareFormat = 'Gi';
$dateCompareFormat = 'Ymd';
function getJson($url) {
// grab the contents of the service call and decode the json object
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_TIMEOUT, 2);
$data = curl_exec($curl_handle);
curl_close($curl_handle);
$c = json_decode($data);
return $c;
}
function getHours($date, $location) {
global $host;
$formattedDate = date('Y-m-d', $date);
$url = $host . '/showLibraryHoursForDate.do?location=' . $location . '&date=' . $formattedDate . '&output=json';
$todaysHours = getJson($url);
return $todaysHours;
}
function openingTime($location) {
global $now, $timeCompareFormat;
$todaysHours = getHours($now, $location);
$openingTime = $todaysHours->startTime;
return $openingTime;
}
function openNow($location) {
global $now, $timeCompareFormat;
$todaysHours = getHours($now, $location);
$startTimeCompare = date($timeCompareFormat, $todaysHours->startTime);
$endTimeCompare = date($timeCompareFormat, $todaysHours->endTime);
$overrideFlag = $todaysHours->override;
// Check if closed all day or open but already passed closing time
if ($overrideFlag == 2 || ($endTimeCompare < date($timeCompareFormat, $now)) {
return false;
}
else {
return true;
}
}
function stillOpenFromYesterday($location) {
global $now, $timeCompareFormat;
$yesterdaysHours = getHours(strtotime('yesterday', $now), $location);
$yesterdayClose = $yesterdaysHours->endTime;
$yesterdayCloseCompare = date($timeCompareFormat, $yesterdayClose);
if ($yesterdayCloseCompare < 600 && date($timeCompareFormat, $now) < $yesterdayCloseCompare) {
return $yesterdayClose;
}
else {
return false;
}
}
function closedToday($location) {
global $now, $timeCompareFormat;
$todaysHours = getHours($now, $location);
$overrideFlag = $todaysHours->override;
if ($overrideFlag == 2) {
return true;
}
else {
return false;
}
}
function closingTime($location) {
global $now, $timeCompareFormat;
$todaysHours = getHours($now, $location);
$stillOpenFromYesterday = stillOpenFromYesterday($location);
if (!$stillOpenFromYesterday) {
$closingTime = $todaysHours->endTime;
}
else {
$closingTime = $stillOpenFromYesterday;
}
return $closingTime;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment