source: subversion/applications/mobile/healthwhere/inc_openclosed.php @ 34887

Last change on this file since 34887 was 21241, checked in by avantman42, 10 years ago

Removed source column in OS-created postcode DB. No need for it, and should make DB file smaller
Now using OS postcode data (OS OpenData? Code-Point® Open)
Re-licensed (GPL v2) because PHPCoord is licensed under GPL v2

  • Property svn:executable set to *
File size: 4.1 KB
Line 
1<?php
2/*
3Healthwhere, a web service to find local pharmacies and hospitals
4Copyright (C) 2009-2010 Russell Phillips (russ@phillipsuk.org)
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License along
17with this program; if not, write to the Free Software Foundation, Inc.,
1851 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19*/
20
21/*
22 * Functions to determine if a shop etc is open now. Main function is
23 * OpenClosed - the other functions are called from OpenClosed
24 * Takes string as defined at http://wiki.openstreetmap.org/wiki/Key:opening_hours
25 * Limitations: Ignores month & monthday
26*/
27
28/*
29 * Function to determine if current time is in the time range
30 * Returns True if current time is in time range, False if not
31 * $timerange: time text (eg "09:00-18:00", "09:30-12:00")
32*/
33function CheckTime ($timerange) {
34        //Split into start & stop times
35        $startstoptimes = explode ("-", $timerange);
36        //Convert start time into minutes since midnight
37        $hourmin = explode (":", $startstoptimes [0]);
38        $startmins = ($hourmin [0] * 60) + ($hourmin [1]);
39        //Convert stop time into minutes since midnight
40        $hourmin = explode (":", $startstoptimes [1]);
41        //Convert 00:xx to 24:xx so that $stopmins is calculated correctly
42        if (intval ($hourmin [0]) == 0)
43                $hourmin [0] = 24;
44        $stopmins = ($hourmin [0] * 60) + ($hourmin [1]);
45        //Get current time as minutes since midnight
46        $iHourOffset = $_GET ["selHourOffset"];
47        $currenttime = getdate ();
48        $currentmins = (($currenttime ["hours"] + $iHourOffset) * 60) + ($currenttime ["minutes"]);
49        //Determine if current time is in the range
50        if ($currentmins >= $startmins && $currentmins <= $stopmins)
51                return True;
52        else
53                return False;
54}
55
56/*
57 * Function to determine if facility is open now
58 * Returns True if it is open, False if it is closed, empty string if not known
59 * $open_hours: value of opening_hours tag
60*/
61function OpenClosed ($open_hours) {
62        //simplest case
63        if ($open_hours == "24/7")
64                return True;
65
66        //Set up array of days as numbers - easier to compare as numbers
67        $weekdays = array ("mo"=>1, "tu"=>2, "we"=>3, "th"=>4, "fr"=>5, "sa"=>6, "su"=>7);
68        //Get today's day as a number
69        $today = $weekdays [strtolower (substr (date ("D"), 0, 2))];
70        //Default to returning False
71        $bOpen = False;
72        //If opening hours aren't specified, return empty string
73        if ($open_hours == "")
74                return "";
75
76        //Ensure $open_hours is lower case - makes life simpler
77        $open_hours = strtolower ($open_hours);
78        //split by semi-colons: each one is a set of days & times
79        $days = explode (";", trim ($open_hours));
80        //Check each set of days/times
81        foreach ($days as $day) {
82                $daytimes = explode (" ", trim ($day));
83                //Turn $daytimes [0] into start & end days
84                $dayrange = explode ("-", $daytimes [0]);
85                $minday = $weekdays [$dayrange [0]];
86                if (count ($dayrange) == 1)
87                        //Single day, so maxday is same as minday
88                        $maxday = $weekdays [$dayrange [0]];
89                else
90                        //Two days
91                        $maxday = $weekdays [$dayrange [1]];
92                //Determine if today is in the range
93                if ($today >= $minday && $today <= $maxday) {
94                        //Today is in the day range - check the time
95                        $times = explode (",", $daytimes [1]);
96                        //Day off - specific case. Return from function immediately
97                        if ($times [0] == "off")
98                                return False;
99                        if ($today == $minday && $today == $maxday) {
100                                /*
101                                 * This is a specific day. Reset $bOpen to false,
102                                 * run CheckTime and return from function without
103                                 * further checks
104                                */
105                                $bOpen = False;
106                                //Check each time
107                                foreach ($times as $time)
108                                        if (CheckTime ($time))
109                                                $bOpen = True;
110                                return $bOpen;
111                        }
112                        else {
113                                //Check each time
114                                foreach ($times as $time)
115                                        if (CheckTime ($time))
116                                                $bOpen = True;
117                        }
118                }
119        }
120        return $bOpen;
121}
122?>
Note: See TracBrowser for help on using the repository browser.