Skip to content

Instantly share code, notes, and snippets.

@ericjames
Created March 12, 2018 17:42
Show Gist options
  • Save ericjames/222d93dc803204dc5744de5df295e6c1 to your computer and use it in GitHub Desktop.
Save ericjames/222d93dc803204dc5744de5df295e6c1 to your computer and use it in GitHub Desktop.
Parse Hours in PHP
<?php
function parseHours($value) {
$hours = json_decode($value);
$hours = (array)$hours;
foreach ($hours as $day => $hourstring) {
$time = explode("-", $hourstring);
$time[0] = trim(str_replace(' ', '', $time[0]));
if (isset($time[1])) {
$time[1] = trim(str_replace(' ', '', $time[1]));
$hours[$day] = [$time[0] => $time[1]];
} else {
$hours[$day] = [$time[0] => ''];
}
}
// current or user supplied UNIX timestamp
$timestamp = time(file_get_contents('getusertime.html'));
// default status
$status = 'closed';
$message = 'Closed';
// get current time object
$currentTime = (new DateTime())->setTimestamp($timestamp);
$currentTimeP1D = (new DateTime())->setTimestamp($timestamp);
$currentTimeP2D = (new DateTime())->setTimestamp($timestamp);
// loop through time ranges for current day
foreach ($hours[$currentTime->format('D')] as $repStartTime => $repEndTime) {
// If times werent entered, stop here
if (!trim($repStartTime)) {
break;
}
// If closed all day
$tomorrow = $currentTimeP1D->add(new DateInterval('P1D'))->format('D');
$overmorrow = $currentTimeP2D->add(new DateInterval('P2D'))->format('D');
$tomorrowStartTime = $hours[$tomorrow];
$overmorrowStartTime = $hours[$overmorrow];
// We allow 'closed' to be passed
if (is_string($repStartTime)) {
if (strtolower($repStartTime) == "closed") {
$message = 'Closed today, opens tomorrow at ' . current(array_keys($tomorrowStartTime));
break;
}
}
if (strlen($repStartTime) < 7) {
if (strpos($repStartTime, ':') === false) {
$repStartTime = DateTime::createFromFormat('hA', $repStartTime);
$repStartTime = $repStartTime->format('h:i A');
} else {
$repStartTime = DateTime::createFromFormat('h:iA', $repStartTime);
$repStartTime = $repStartTime->format('h:i A');
}
if (strpos($repEndTime, ':') === false) {
$repEndTime = DateTime::createFromFormat('hA', $repEndTime);
$repEndTime = $repEndTime->format('h:i A');
} else {
$repEndTime = DateTime::createFromFormat('h:iA', $repEndTime);
$repEndTime = $repEndTime->format('h:i A');
}
}
// create time objects from start/end times
$startTime = DateTime::createFromFormat('h:i A', $repStartTime);
$endTime = DateTime::createFromFormat('h:i A', $repEndTime);
$endTime2H = DateTime::createFromFormat('h:i A', $repEndTime);
$endTime2H->add(new DateInterval('PT2H'));
// Add 1 day to endTimes less than startTime
if ($endTime < $startTime) {
$endTime->add(new DateInterval('P1D'));
$endTime2H->add(new DateInterval('P1D'));
}
if (($startTime <= $currentTime) && ($currentTime <= $endTime)) {
$status = 'open';
$message = 'Open until ' . $endTime->format('g:i A');
break;
} else {
// set default
$message = 'Closed now, opens at ' . $startTime->format('g:i A');
}
// if (($currentTime > $endTime) && ($currentTime < $endTime2H)) {
// $message = 'Closed at ' . $endTime->format('g:i A');
// break;
// }
}
$value = json_decode($value);
$value->status = $status;
$value->message = $message;
return json_encode($value);
}
@ericjames
Copy link
Author

Use this for the user file <script>document.write(Math.floor(Date.now() / 1000));</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment