Skip to content

Instantly share code, notes, and snippets.

@MiniCodeMonkey
Created August 20, 2012 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MiniCodeMonkey/3407136 to your computer and use it in GitHub Desktop.
Save MiniCodeMonkey/3407136 to your computer and use it in GitHub Desktop.
<?php
public function getOpenStoresCount()
{
$textualDateCondition = OpeningHour::getTextualDateQuery(); // Use the helper function from OpeningHour
$query = "SELECT
COUNT(*)
FROM `stores`
WHERE id IN (
SELECT store_id FROM opening_hours WHERE date = CURDATE()
AND
(
(open_time < CURTIME() AND close_time > CURTIME())
OR
(open_time = MAKETIME(23, 59, 00) AND close_time = MAKETIME(00, 01, 00))
)
UNION
".''/* ...or todays weekday is an opening hour */."
SELECT store_id FROM opening_hours WHERE (". $textualDateCondition .")
AND
(
(open_time < CURTIME() AND close_time > CURTIME())
OR
(open_time = MAKETIME(23, 59, 00) AND close_time = MAKETIME(00, 01, 00))
)
AND store_id NOT IN ".''/* Select ONLY if we haven't already registered the store as open using date */."
(
SELECT store_id FROM opening_hours WHERE date = CURDATE()
AND
(
(open_time < CURTIME() AND close_time > CURTIME())
OR
(open_time = MAKETIME(23, 59, 00) AND close_time = MAKETIME(00, 01, 00))
)
)
)
AND parser_id IN (SELECT id FROM parsers WHERE active=1)
AND country = 'Denmark'";
$sth = Database::getInstance()->prepare($query);
$sth->execute(array());
if ($row = $sth->fetch())
{
return $row[0];
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment