Jump to content

date and time loops


greenApple

Recommended Posts

I am working on a simple project that will allow users to book an appointment. My idea is to create a weekly calendar as a table, with the days of the week going across the top and time going down the left side.This will give me the days of the week:

<?php	$x = 1;	echo "<table border='1'><tr>";	echo "<td>";	echo "Time";	echo "</td>";	while($x < 7)	{		echo "<td>", date('y-m-d', strtotime(date('Y').'W'.date('W').$x)), "</td>";		$x++;	}		echo "</tr>";	echo "<td>";	TIME LOOP HERE	echo "</td>";	echo "</tr>";		echo "</table>";?>

However, I don't know where to go from here, I need to be able to select the time and day and then check it against a database to see if it is available.Also I have not been able to come up with a loop which will show the times. I need it to start at 9am with 30 minute intervals and loop until 5pm. I can create a number loop with number but I can't get it to work for time.Can anyone give me tips or code snippets that could help me?!Thanks!!

Link to comment
Share on other sites

if i am not missunderstanding your situation..then...take the hours as minutes..increase it (using loop) by 30 minutes...manupulate a table of time and date..which will be able to pass data in another page (may be propogating the data by GET)take date month year time hour minute and pass it to a page to porocess..using those data make a timesamp..using mktimenow check the database for timestamp..beetwwen the generated time stamp..and timestamp+(30*60) seconds..it will check in your databse that which fields are coming beeweeen those timestamp mark(timestamp and timestamp+30*60 seconds)and if it able macthes the timestamp in your database (which is stored as event) you will get it..then you can fetch and format acording to your needs.i am assuming you are storing the time as timestamp in db..and want to check event based on every 30 minute slot of a daythere may be more things need to do..which may be i am missing..or missunderstanding

Link to comment
Share on other sites

birbals on the right track. In your database you should have timestamps recording the start time of the appointment, and the end time of the appointment. For each day, you need to create a loop, that starts at 9am on that day in the year, and increments every half hour. It will probably be easier to focus on one day at a time for this, before trying to render all 5 days at once. As you go through this process, each 30 minutes interval will have it's own unique time stamp. You will need to cross reference this "on-the-fly" timestamps with the the one's in your DB to find out whether a block is free or not. One way is to retrieve all the time stamps for that particular day in time from the db who's appointments start on or after 9am, and and on or before 5pm. You could have them returned in ascending order, so as you loop through the day's worth of appointments, you will have everything order properly, starting from first appointment of the day, to the last. Once you get a good system in place to do one day (Monday), then you could expand outward to the other four days. At this time you will probably want to change how the records are retrieved from the database so that you can look up the entire week at once, and get all the sorted appointment for that week returned in a nice convenient object; probably an array of objects, with each index representing an appointment, with it's relevant information, like start and end time, and maybe any other related info that may be stored in the DB. Are planning on allowing people to go into the future to make appointments? Are you planning on doing this via AJAX when the page loads, or when (and if) they can go to other weeks to view appointments?

Link to comment
Share on other sites

take the hours as minutes..increase it (using loop) by 30 minutes...manupulate a table of time and date..which will be able to pass data in another page (may be propogating the data by GET)take date month year time hour minute and pass it to a page to porocess..using those data make a timesamp..using mktimenow check the database for timestamp..beetwwen the generated time stamp..and timestamp+(30*60) seconds..it will check in your databse that which fields are coming beeweeen those timestamp mark(timestamp and timestamp+30*60 seconds)and if it able macthes the timestamp in your database (which is stored as event) you will get it..then you can fetch and format acording to your needs.i am assuming you are storing the time as timestamp in db..and want to check event based on every 30 minute slot of a day
Thanks for giving me these steps, it's hard sometimes to break down a project I find. My PHP coding skills aren't great so I will probably be back for some help!
Are planning on allowing people to go into the future to make appointments?
Ideally I would like people to be able to make appointments 2 to 3 weeks in advance.
Are you planning on doing this via AJAX when the page loads, or when (and if) they can go to other weeks to view appointments?
I would like to use AJAX.Thanks!!
Link to comment
Share on other sites

I am still having trouble with the actual coding of this. I have gotten this far:

<html><head><script type="text/javascript" src="popup-window.js"></script></head><body><?php		$connect = mysql_connect("xxxxxxxxxxxxxxxx") or die("Can't connect");	mysql_select_db("xxxxx") or die("cannot find database");		//extract data for drop down box	$extract = mysql_query("SELECT treatment FROM treatment") or die(mysql_error());	$numrows = mysql_num_rows($extract);		$x = 1;	echo "<table border='1'><tr>";	echo "<td>";	echo "Time";	echo "</td>";	while($x < 7)	{		echo "<td>", date('y-m-d', strtotime(date('Y').'W'.date('W').$x)), "</td>";		$x++;	}		echo "</tr>";		echo "<tr>";	echo "<td>";	[b]echo TIME LOOP HERE;[/b]	        echo "</td>";	echo "<td>";?>	<form action="" onsubmit="return false;">		<div>			<input type="button" value="Book" onclick="popup_show('popup', 'popup_drag', 'popup_exit', 'mouse', -10, -10);" />		</div>	</form>	<div class="sample_popup"     id="popup" style="display: none;">	<div class="menu_form_header" id="popup_drag">		<img class="menu_form_exit"   id="popup_exit" src="form_exit.png" alt="" />		   Book	</div>	<div class="menu_form_body">		<form action="cal.php">		<table>			<tr>				<th>Date</th>					<td>[b]<?php echo $date; ?>[/b]</td>			</tr>					<tr>				<th>Time</th>				<td>[b]<?php echo $time; ?>[/b]</td>			</tr>					<tr>				<th>Treatment</th>					<td><?php					echo "<select name= 'treatment'>";					while ($row = mysql_fetch_assoc($extract))					{					$treatment = $row['treatment'];							echo "					<option>$treatment</option>					";					}  //end while loop							echo "</select>";				?></td>			</tr>			<tr><th>         </th><td><input class="btn"   type="submit"   value="Submit" /></td></tr>		</table>		</form>	</div>	</div>		<?php			echo "</td>";			echo "</tr>";			echo "</table>";		?></body></html>

I can get the dates of the week and display them across the top of the table but I cannot figure out how to grab the date when I select that day and set it as a variable ($date) to be saved to the database. How can I do this?Also, I am having serious trouble creating the time loop. I can't figure out how to create the loop that will keep adding 30mins from 9am until it reaches 5pm. Does anyone have any code examples? How can you add time?

Link to comment
Share on other sites

Also, I am having serious trouble creating the time loop. I can't figure out how to create the loop that will keep adding 30mins from 9am until it reaches 5pm. Does anyone
assuming 12 am as start ..mean zero9 am=60*95pm=60*17now...it should be something like....
for($time=60*9,$time<=60*24,$time+30){//show the time slots// take the time as minute and format as hour:minute base to show as link(store in a var) and somepage.php?time=$time to pass the value }

I can get the dates of the week and display them across the top of the table but I cannot figure out how to grab the date when I select that day and set it as a variable ($date) to be saved to the database. How can I do this?
its depend on how you planing to send the data to other page...
Link to comment
Share on other sites

you need to assighn the $time variable in for loop..sorry i missed it at first post.

<?phpfor($time=60*9;$time<=60*17;$time=$time+30){echo date("h:i", $time);}?>

here the time is not timestamp actually..its minute form of one day..whic we assumed that start from 0 so you need to take out the hour and minute

for(......){$hour=$time/60;$minute=$time%60;echo "<a href='somepage.php?hour=$hour&minute=$minute'>$hour</a>";}

now it will pass the value to somepage.php where you can process the inputs. it is also possible with ajax also..

Link to comment
Share on other sites

thank you so much for your help!!!! Instead of a link I would like the variables to be sent to a Javascript pop up, my attempt is below, but this will only ever grab the first hour of the day - 9:00. If I click on the 10:00 link it will still grab 9:00 :)

	                  <form name='book' method='POST' onsubmit="return false;" action="?hour=<?php echo $hour;?>&minute=<?php echo $minute;?>">		<div>			<input type="button" value="Book" onclick="popup_show('popup', 'popup_drag', 'popup_exit', 'mouse', -10, -10);" />		</div>	        </form>	         <div class="sample_popup"     id="popup" style="display: none;">              	<div class="menu_form_header" id="popup_drag">		<img class="menu_form_exit"   id="popup_exit" src="form_exit.png" alt="" />		   Book	       </div>	       <div class="menu_form_body">		<form action="cal.php">		<table>			<tr>				<th>Date</th>					<td><?php echo $date; ?></td>			</tr>					<tr>				<th>Time</th>				<td><?php echo $hour; echo $minute;?></td>			</tr>					<tr>				<th>Treatment</th>					<td><?php					echo "<select name= 'treatment'>";					while ($row = mysql_fetch_assoc($extract))					{					$treatment = $row['treatment'];							echo "					<option>$treatment</option>					";					}  //end while loop							echo "</select>";				?></td>			</tr>			<tr><th>         </th><td><input class="btn"   type="submit"   value="Submit" /></td></tr>		</table>		</form>	</div>	</div>

Link to comment
Share on other sites

put your form tag (which is passing the inputs via js popup) inside the for loop

Link to comment
Share on other sites

OK I think you might kill me now!! The issues I am having:1) Currently the time variable is being selected when you click on a link, but I need the variables to be selected as the page loads and compare the time to a database to see if the time is available, only if it is available should you be able to click on the link to book the appointment.2) The time is not displaying incorrectly, it is displaying as .5 of an hour, I need it to be in minutes eg. 9:30 not 9.5

Link to comment
Share on other sites

1) i thought you want to check it each time slot seprately that it has event or not...if you want to list out the the times and want to show that each timse slot status (event have or dont have) then i think it will be better to do something like this..1)user click on a date ..pass the date year month2)make time stamp for that day started from eg. 5pm3)check that time stamp existance in database slot of the whole day started from 5pm timestamp for that day to +minutes how far you want) a) if no row exist ( mean 0) there is no event b)else fetch the result set take out the apointment data in array4)now loop through increasing 30 minutes started from the time stamp in line number (2)5)in the loop cross check that any timestamp (from database array) are mtaching into that current slot or not a)if match show that no apointment available b)else a link for make a apointment.this will do lis out the status of the days..now as same way the link (check line 5.:) pass the data to make a apointment..and in other page you take those data and process it to create a aponitment for that time.2) you need to do type jugling or maybe floor()

 echo (int)$hour;

or

echo floor($hour);

it will give you the hour and rest one ($minute) which give you minute.it should work now i think...

Link to comment
Share on other sites

you need to do type jugling or maybe floor()
<?php		for($time=60*9;$time<=60*17;$time=$time+30)		{		$hour=$time/60;		$minute=$time%60;		                echo "<table border='1'><tr><td>";		echo $hour;				echo"</td>";		echo"<td>";                echo "<a href='somepage.php?hour=$hour&minute=$minute'>available</a>";                echo"</td></table>";                }?>

Link to comment
Share on other sites

I can't get this to work.I have just noticed something else, the code below is the for loop for the time, 9 works ok as the $hour=9 and $minute=0 but if I click on 9.5 $hour=9.5 and $minute=30. How can I get $hour not to grab the decimal place?
this is the way..it will take the 9 from 9.5
echo (int)$hour;

echo floor($hour);

more details..http://php.net/language.type_juglinghttp://php.net/function.floor

Link to comment
Share on other sites

  • 2 weeks later...
each 30 minutes interval will have it's own unique time stamp.
How can I go about this?This is how I have it set up:Each appointment has a date, a start time, a treatment and a treatment length (15, 30, 45, 60 mins). So I see it working that I add the treatment length to the start time and this will give me an end time.this is my current code, where the start time is $hour and $minute.
<?php        //get variables        $year = $_GET["year"];	$month = $_GET["month"];	$day = $_GET["day"];	$hour = $_GET["hour"];	$minute = $_GET["minute"]; 	        $treatment = $_POST["treatment"];		$hour = (int)$hour;                //get treatment length        $length = mysql_query("SELECT treatmentLength FROM treatment WHERE treatment='$treatment'");				while ($row = mysql_fetch_assoc($length))//place into an array called $row			{				$treatmentLength = $row['treatmentLength'];			}         ?>

The problem I am having is that I need to make the $hour and $minute into one variable ($startTime) and then add $treatmentLength to this. To create one start time variable

$startTime = ($hour.$minute);

If the time selected is 9:15am, this code will give me 915, however the treatmentLength taken from the database is in 00:00:00 format, so for example is a treatment is 45mins it will be 00:45:00. How can I put the 9:15am in the correct format and add treatmentLength to it to create $endTime?

it will take the 9 from 9.5
Thank you for all your help!! I really appreciate it!
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...