Skip to content

Instantly share code, notes, and snippets.

@felipsmartins
Created October 27, 2020 16:02
Show Gist options
  • Save felipsmartins/7554a28e5e36e1ae66713a9e8509a280 to your computer and use it in GitHub Desktop.
Save felipsmartins/7554a28e5e36e1ae66713a9e8509a280 to your computer and use it in GitHub Desktop.
<?php
define('DEFAULT_TIMEZONE', 'America/Fortaleza');
define('WORKING_DAY_MONDAY', 1);
define('WORKING_DAY_TUESDAY', 2);
define('WORKING_DAY_WEDNESDAY', 3);
define('WORKING_DAY_THURSDAY', 4);
define('WORKING_DAY_FRIDAY', 5);
define('WORKING_DAY_SATURDAY', 6);
define('WORKING_DAY_SUNDAY', 7);
$opening_hours_config = [
'monday_friday' => [
'begin' => '8:00',
'end' => '16:00',
],
'saturday' => [
'begin' => '8:00',
'end' => '12:00',
],
];
/**
* @return bool
* @throws Exception
*/
function is_opening_hour()
{
global $opening_hours_config; # bad!
$timezone = new DateTimeZone(DEFAULT_TIMEZONE);
$now = new DateTimeImmutable('now', $timezone);
$weekday = $now->format('N');
$opening_hours = null;
if (in_array($weekday, [
WORKING_DAY_MONDAY,
WORKING_DAY_TUESDAY,
WORKING_DAY_WEDNESDAY,
WORKING_DAY_THURSDAY,
WORKING_DAY_FRIDAY,
])) {
$opening_hours = $opening_hours_config['monday_friday'];
}
if (WORKING_DAY_SATURDAY == $weekday) {
$opening_hours = $opening_hours_config['saturday'];
}
# sunday
if (is_null($opening_hours)) {
return false;
}
# is opening? :)
$begin = new DateTimeImmutable($opening_hours['begin'], $timezone);
$end = new DateTimeImmutable($opening_hours['end'], $timezone);
if ($now >= $begin && $now <= $end) {
return true;
}
return false;
}
var_dump(is_opening_hour());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment