Date and Time PHP/HTML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Paya
Forum Newbie
Posts: 4
Joined: Wed Mar 09, 2011 8:22 am

Date and Time PHP/HTML

Post by Paya »

Hi all,

I've been working on my website to create a booking page. The only thing i've got left is the validation for the time and date. They are drop down boxes.
The time consists of only 1 drop down box and the date consists of 3 drop down's (day-month-year).

My problem is that I know in plain English what the box needs to do but cause of a lack of skill I cant figure out how to code the validation for it. (and yes I have searched all google for this and I know that there are some validations but not the ones that can help me with the way I implemented my drop down boxes)

For the time I want to validate that the user cannot choose a time which has already been booked on the same date as someone else.
For the date I want to validate that the user cannot choose a date which is less or equal to the current date.

I'm still in my learning progress so any help is appreciated. I can show the validation which I have already made for the booking so you lot know what level of coding is..

Code: Select all

require_once("connect.php");


if (empty($_POST["gp"]) && (empty($_POST["nurse"])))
{
echo("Error! Please select a nurse or a doctor <br /><br />
		<a href='booking.php'><b>Please try again</b></a>.");
}

else if (!empty($_POST["gp"]) && (!empty($_POST["nurse"])))
{
echo("Error! Please select ONLY a nurse or a doctor <br /><br />
		<a href='booking.php'><b>Please try again</b></a>.");
}

else if (empty($_POST['username']))
{
echo("Please enter your username<br /><br />
		<a href='booking.php'><b>Please try again</b></a>.");
}

else if (($_POST['username']) != ($_SESSION['username']))
{
echo ("Error! This username does not exist.<br /><br />
		<a href='booking.php'><b>Please try again</b></a>.");
}

else{
	// Book the appointment.
	$query = mysql_query("INSERT INTO appointment 
	(appointmentID, username, day, month, year, time, gp, nurse)
	VALUES	('NULL', '".$_POST['username']."', '".$_POST['day']."', '".$_POST['month']."','".$_POST['year']."', '".$_POST['time']."', '".$_POST['gp']."', '".$_POST['nurse']."')")
	or die (mysql_error());
	
	echo "You have successfully booked an appointment!<br /><br />
			To go back to the home page click <a href='indexmembers.php'><b>here</b></a>.";
	}
And this is the booking layout to make it easier to understand:
Image

Thanks
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

Re: Date and Time PHP/HTML

Post by incubi »

Hi Paya,

Is there a way you can change they way you are storing time/dates from the individual units to a timestamp or one db field using the date/time format? If not you will need to convert the date/time values to a timestamp and compere them then convert it back before you add it to the database. I can help whichever way but the first is less work so if you can do it lets start there.
In MySQL its easy to compare dates/times if the field is set correctly. In PHP its easy if the value is a time stamp.
Paya
Forum Newbie
Posts: 4
Joined: Wed Mar 09, 2011 8:22 am

Re: Date and Time PHP/HTML

Post by Paya »

Hi incubi,

thanks for your reply. Well my original database format existed with only a DATE and TIME attribute, so I could change this back to the original state in the table, but I would have no clue how to code 3 drop down boxes into a single attribute though..That is the reason I separated the DATE attribute into 3 parts (day, month and year)
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

Re: Date and Time PHP/HTML

Post by incubi »

I use a javascript date/time picker much like this one.

http://www.javascriptkit.com/script/scr ... ndar.shtml

This will get the date and time in one field when the user picks it.

*Edit*

However you can do this after the form is submitted.

$mynewtime = $_POST['year']."-".$_POST['month']."-".$_POST['day']." ".$_POST['time'];


Lee
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

Re: Date and Time PHP/HTML

Post by incubi »

Also, you may want to change your code like this

Code: Select all

<?php


//In your form add a hidden field that tels this part of the php code to run when the form is submitted like this,
//	<INPUT TYPE=HIDDEN NAME="addbooking" value="go">
//This way you can control when this runs. 


if ($_GET['addbooking] == "go"): 

require_once("connect.php");

	if (empty($_POST["gp"]) && (empty($_POST["nurse"])))
	{
		echo("Error! Please select a nurse or a doctor <br /><br />
               <a href='booking.php'><b>Please try again</b></a>.");
		exit(0);	   
	}
	if (!empty($_POST["gp"]) && (!empty($_POST["nurse"])))
	{
		echo("Error! Please select ONLY a nurse or a doctor <br /><br />
                <a href='booking.php'><b>Please try again</b></a>.");
		exit(0);	   		
	}
	if (empty($_POST['username']))
	{
		echo("Please enter your username<br /><br />
                <a href='booking.php'><b>Please try again</b></a>.");
		exit(0);	   		
	}
	if (($_POST['username']) != ($_SESSION['username']))
	{
		echo ("Error! This username does not exist.<br /><br />
                <a href='booking.php'><b>Please try again</b></a>.");
		exit(0);	   		
	}
	
       // Book the appointment.
    $query = mysql_query("INSERT INTO appointment 
	(appointmentID, username, day, month, year, time, gp, nurse)
     VALUES  ('NULL', '".$_POST['username']."', '".$_POST['day']."', '".$_POST['month']."','".$_POST['year']."', '".$_POST['time']."', '".$_POST['gp']."', '".$_POST['nurse']."')")
     or die (mysql_error());
       
    echo "You have successfully booked an appointment!<br /><br />
                        To go back to the home page click <a href='indexmembers.php'><b>here</b></a>.";
						
    
endif;

?>
Where you were doing IF and ELSE IF the outcome would be that only the first IF would be evaluated if
$_GET['getruns']) && $_GET['getruns'] are true. You want to evel every field even if one is filed out.

:)

Lee
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

Re: Date and Time PHP/HTML

Post by incubi »

As an example of using only one date/time field.

Code: Select all


	/* 
	The code below will work if you use one SQL field as your date and time. The field must be a datetime Type as
	noted here http://dev.mysql.com/doc/refman/5.0/en/datetime.html
	*/
		
		//If result is empty then no one booked the time and date so book it. 
	$sql = "SELECT * FROM appointment WHERE date = '$sysdate' ORDER BY date";
	$result = mysql_query($sql);
	if(mysql_num_rows($result) == NULL)
	{
		// Book the appointment.
		$query = mysql_query("INSERT INTO appointment 
		(appointmentID, username, day, month, year, time, gp, nurse)
		VALUES  ('NULL', '".$_POST['username']."', '".$_POST['datetime']."', '".$_POST['gp']."', '".$_POST['nurse']."')")
		or die (mysql_error());
       
		echo "You have successfully booked an appointment!<br /><br />
            To go back to the home page click <a href='indexmembers.php'><b>here</b></a>.";
	}
    else
	{
		echo ("Sorry time is not available.<br /><br />
                <a href='booking.php'><b>Please try again</b></a>.");
		exit(0);	   		
	}	
	
I didn't test this just banged it out but that's the idea.

Lee
Paya
Forum Newbie
Posts: 4
Joined: Wed Mar 09, 2011 8:22 am

Re: Date and Time PHP/HTML

Post by Paya »

Thanks for the great explanation Lee. I will try to apply your methods and see if I can get it working. I'll get back to you soon!

Thanks again!
Post Reply