Store Open or Closed
I would like to be able to create a script that displays whether or not a store is open when the user looks at the page. It would be simple if some didn't close for an hour during the day.
I could set it up:
MondayOpen 800
MondayClosed 1600
and have the script determine if the current time is in between those two times.
However, like I said, some stores are closed for a little time during the day. I'm at a loss how I should set up my tables and script.
Any help would be greatly appreciated.
ptara1
Junior Poster in Training
67 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
You may need to sort out timezones too. Those times could be meaningless unless the user shares the same timezone. Also, is the server time TZ the same as the store TZ? It all gets very untidy unless TZs are tied down.
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
@blocblue - yes clear thinking, me cloudy as usual.
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
Thanks a million, definitely moving in the right direction...
Now my problem is I currently have two separate tables, one for store hours and one for store info. I am using the unique id for store info multiple times within the store hours table. How can I run another mysql select statement with this to select records from the store info table? I'm going to keep working on figuring it out on my own but any help would be greatly appreciated.
Also, how do I configure the server to match the timezone for where the stores are? I didn't even think about that problem...Shows how little I actually know...
Seriously though the help is amazing.
ptara1
Junior Poster in Training
67 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
pritaeas
Posting Expert
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
what happens if the store is open past midnight or 0:00:00 hours. Will the script still work?
ptara1
Junior Poster in Training
67 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
If all of your stores are in one country with one timezone, just use date_default_timezone_set to set your own TZ to that so everybody is then synchronized (as noted by blocblue).
However, if stores across a few timezones, you could be OK if none of them use a DST (daylight saving time). This means that they may move an hour forward/backward at certain times of the year in order to get more light in the evening/morning. If no DSTs, you could have a DB tinyint (or float) field with the timediffs from your server time on it, usu. in hours (or half hours).
If those TZs or your server time use DST, you'll need a more robust method. E.g. the UK is currently in DST mode, so it is not the same as using GMT/UTC.
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
Alright, I have the time zone figured out. Need to continue my own testing to see if it works when it's open past 0:00:00. Thanks for all your help
ptara1
Junior Poster in Training
67 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
Ok this is the first time I've tried to run multiple selects from different tables so I'm having a little bit of a hard time.
Here is my code, I get one response returned. What should I tweak it to get all the results returned?
<?
date_default_timezone_set('America/Chicago');
include 'connect.php';
$day = date('l');
$time = date('H:i:s');
$sql = sprintf("SELECT `ID` FROM `storehours` WHERE `Day` = '%s' AND '%s' BETWEEN `Open` AND `Close`", $day, $time);
$result=mysql_query($sql);
while($row = mysql_fetch_array($result)){{
$ID=$row['ID'];
}
if (mysql_num_rows($result)>0) {
echo "
<table width='700px' border='0px' cellspacing='5px' cellpadding='5px'>
<tr>
<th><font face='Arial, Helvetica, sans-serif'>Name</font></th>
<th><font face='Arial, Helvetica, sans-serif'>Phone Number</font></th>
<th><font face='Arial, Helvetica, sans-serif'>Location</font>
</tr>
";
$sql1="SELECT * FROM stores WHERE ID ='".$ID."'";
$result1=mysql_query($sql1);
while($row = mysql_fetch_array($result1))
{
$Name = $row['Name'];
$Phone_Number = $row['Phone_Number'];
$Location = $row['Location'];
echo "<tr><td>";
echo $Name;
echo "</td><td>";
echo $Phone_Number;
echo "</td><td>";
echo $Location;
echo "</td></tr>";
echo "</table>";
}
}
else {
echo "No Stores Open";
}
}
?>
Any help will be greatly appreciated.
ptara1
Junior Poster in Training
67 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
Ok you want a join clause so that you just need to use one sql query. Also your braces look a bit odd. As soon as you finish your while loop, your id will point to the last record.
BTW - do storehours and stores have to be in different tables? Seems to me that the data belongs together. ANyway:
I'd do this:
<?php
$sql = mysql_query("..."); //use a join clause for tables storehours and stores
$tbldata = "";
if(mysql_num_rows($sql) > 0){
while($data = mysql_fetch_assoc($sql)){
$tbldata .= "<tr><td>{$data['name']}</td><td>...</td><td>...</td></tr>";
}
}else{
$tbldata = "<tr><td colspan=\"3\">No stores open</td></tr>";
}
?>
Put your table html header...
<?php echo $tbldata;?>
Put your table footer...
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
Awesome, I was thinking that I was going to have to do a join statement but wasn't sure.
I've never done a table that way. Generally do it in the way that's illustrated in my code.
When you say $tbldata = ""; what should be put there?
ptara1
Junior Poster in Training
67 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
Nothing. It just initialises the variable. Some sites throw a wobbly if you try to concatenate (.=) a string that doesn't exist yet. Don't know why, but I've found that this works.
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
I've been trying to do a join table and having some trouble with mysql statement. Here is what I have and I keep getting an error.
$result = sprintf("SELECT 'hours.ID', 'store.ID', 'store.Name', 'store.Phone_Number', 'store.Location' FROM 'hours', 'stores' WHERE 'Day' = '%s' AND '%s' BETWEEN 'Open' AND 'Close' AND 'hours.ID' = 'store.ID'", $day, $time);
while($row = sprintf($result))
{
$ID=$row['ID'];
$Name = $row['Name'];
$Phone_Number = $row['Phone_Number'];
$Location = $row['Location'];
}
if (mysql_num_rows($result)>0) {
//working if statement
}
It says "Fatal error: Maximum execution time of 30 seconds exceeded"
Should I still use sprintf or should I use something else?
ptara1
Junior Poster in Training
67 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
$result = mysql_query("SELECT 'hours.ID', 'store.ID', 'store.Name', 'store.Phone_Number', 'store.Location' FROM 'hours', 'stores' WHERE 'Day' = '$day' AND '$time' BETWEEN 'Open' AND 'Close' AND 'hours.ID' = 'store.ID'");
if(mysql_num_rows($result) > 0){
echo "<table><thead><tr><th>ID</th><th>Store</th><th>Phone</th><th>Location</th></tr></thead<tbody>";
while($row = mysql_fetch_assoc($result)){
$ID=$row['ID'];
$Name = $row['Name'];
$Phone_Number = $row['Phone_Number'];
$Location = $row['Location'];
echo "<tr><td>$ID</td><td>$Name</td><td>$Phone_Number</td><td>$Location</td></tr>";
}
echo "</tbody></table>";
}
You could try that. If it doesn't work, look at the SQL.
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
I have tried your fix and am currently getting this error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource
I will keep trying to tweak it and fix it. But any idea why this is occurring
ptara1
Junior Poster in Training
67 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0