Click to See Complete Forum and Search --> : Modifying php code:Daily business hours


sac8513
01-13-2007, 04:30 PM
I have been working on developing code that analyzes a database in mySQL to search for a stores name, when it opens and when it closes.

The php code that I am using also is set to the server time to outut whether that particular store is open or closed. My issue is that I now need to add each day of the week to the code to make it work properly. Any ideas?

Here is what I currently have

$query="SELECT * FROM timetest ORDER BY store_name ASC";
$result=mysql_query($query);
$num=mysql_numrows($result) or die('There are currently no stores in our database.');

echo '<table border="0" cellspacing="4" cellpadding="4" colspan"140">
<tr>
<th><font face="Arial, Helvetica, sans-serif"></font></th>
<th><font face="Arial, Helvetica, sans-serif">Telephone</font></th>
<th style="white-space: nowrap;"><font face="Arial, Helvetica, sans-serif">Hours of Operation</font></th>';

$i=0;

while ($i < $num) {
$name=mysql_result($result,$i,"store_name");
$tele=mysql_result($result,$i,"telephone");
$o=mysql_result($result,$i,"open_time");
$c=mysql_result($result,$i,"close_time");


$op = split(':',$o);
$open = ($op[0]*60)+$op[1];

$cl = split(':',$c);
$close = ($cl[0]*60)+$cl[1];

$sign = "-";
$h = "5";
$dst = "true";

if ($dst) {
$daylight_saving = date('I');
if ($daylight_saving){
if ($sign == "-"){ $h=$h-1; }
else { $h=$h+1; }
}
}

$hm = $h * 60;
$ms = $hm * 60;

if ($sign == "-"){ $timestamp = time()-($ms); }
else { $timestamp = time()+($ms); }

$hour = gmdate("H", $timestamp);
$minutes= gmdate("i", $timestamp);
$now = ($hour*60)+$minutes;

echo '<tr>
<td><font face="Arial, Helvetica, sans-serif"><a href='.$name.'.html>'.$name.'</a></font></td>
<td style="white-space: nowrap;"><font face="Arial, Helvetica, sans-serif">'.$tele.'</font></td>
<td><font face="Arial, Helvetica, sans-serif">';

if(($now>=$open)&&($now<$close))
echo 'Open';

else
echo 'Closed';

echo '</font></td></tr>';
$i++;
}

echo '</table>';

mysql_close();
?>

NightShift58
01-13-2007, 07:15 PM
How are you storing the store's opening and closing times?

sac8513
01-13-2007, 10:13 PM
Currently they are stored under fields "open_time" and "close_time" . But I am having trouble trying to figure out how to set up this up for the days of the week

Now that I think about it though I may have to create a separate table for each store with the opening and closing times for each day of the week. Does this seem efficient? Depending on how many stores I have there could be endless tables. Im not sure of a better way

The Little Guy
01-14-2007, 01:26 AM
you could make one field, and make something like this:

Database Text:
9;9;1;4;6;8


then do something like this


$times = "9;9;1;4;6;8"; /*This will have to contain 14 numbers you could use a letter for days that they are not open at all*/
$time_array = explode(";",$times);

/*One var for each value in the array there will be a total of 14 variables in the list*/
list($mon_op,$mon_cl,$tue_op,$tue_cl,$wed_op,$wed_cl) = $time_array;

echo $mon_op; //prints 9
echo $mon_cl; //prints 9
echo $tue_op; //prints 1
echo $tue_cl; //prints 4
echo $wed_op; //prints 6
echo $wed_cl; //prints 8

sac8513
01-14-2007, 02:32 AM
I am not sure if I understand the database text that you are using. Do these numbers hold any specific meaning or are you just using them simply to show me the purpose of the code?

Also where would I insert this portion into the code above and be able to differentiate between around 40 different stores?

The Little Guy
01-14-2007, 03:17 AM
The numbers stand for the hour, 9:00, 1:00, 4:00 etc.

This would go in the while loop:

$times = "9;9;1;4;6;8"; /*This will have to contain 14 numbers you could use a letter for days that they are not open at all*/
$time_array = explode(";",$times);

/*One var for each value in the array there will be a total of 14 variables in the list*/
list($mon_op,$mon_cl,$tue_op,$tue_cl,$wed_op,$wed_cl) = $time_array; /*pattern continues till var = $fri_cl*/

op = open
cl = close

This would go where ever you want the specific time to display

echo $mon_op; //prints 9
echo $mon_cl; //prints 9
echo $tue_op; //prints 1
echo $tue_cl; //prints 4
echo $wed_op; //prints 6
echo $wed_cl; //prints 8

/*pattern continues till var = $fri_cl*/

NightShift58
01-14-2007, 10:43 AM
More or less a double post... See: http://www.webdeveloper.com/forum/showthread.php?p=696805#post696805