How to validate the variable dates

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
preetgowda
Forum Newbie
Posts: 7
Joined: Wed Mar 31, 2004 4:35 am

How to validate the variable dates

Post by preetgowda »

hi, i have a submit form for a user to input but i want to make sure the date is correct and will be a valid one of the form yyyy-mm-dd. I want to have a range from at least the year 1940 to 2010.
If anyone could help that would be great!

Admin Edit: Added tags to code - use them it makes your code easier to read.[/size][/color]

Code: Select all

<html>
<head><title>Submit form For Album Titles</title></head>

<body text="#000000" bgcolor="#feebcc"> 

<style type="text/css">
  body { margin-left: 10%; margin-right: 10%; }
</style>

<center><img src="unicrest.gif" width="154" height="79"
alt="University crest of Liverpool" longdesc="unicrest.html"></center>

<HR SIZE=3 style="background-color: blue">

<body>

<font face = "verdana">
<font size=4>
  
       <center>
          <a href = "http://www.csc.liv.ac.uk/~u1phg/UserLogin.php">
	   Home Page
	   </a>
	</center>   

<center>
          <a href = "http://www.csc.liv.ac.uk/~u1phg/ContactPage.html">
	   Contact Page
	   </a>
	</center>   


<center>
          <a href = "http://www.csc.liv.ac.uk/~u1phg/InfoPage.html">
	   Information Page
	   </a>
	</center>   
	
	</font size=4>

<h2>Submitting Of Album Title</h2>
<h3>Fields shown in <font color="red">red</font> are mandatory.</h3> 

<?php
	$user="u1phg";
	$host="mysql";
	$password="";
	$database="u1phg";
	$connection = mysql_connect($host,$user,$password) or die ("connection to server failed.");


        $db=mysql_select_db($database,$connection) or die ("could not connect to database");
$albumId=$HTTP_POST_VARS['albumId'];
       $albumTitle=$HTTP_POST_VARS['albumTitle'];
       $classicalTrack=$HTTP_POST_VARS['classicalTrack'];
       $cdNumber=$HTTP_POST_VARS['cdNumber'];
       $artistBandName=$HTTP_POST_VARS['artistBandName'];
       $genre=$HTTP_POST_VARS['genre'];
       $label=$HTTP_POST_VARS['label'];
       $year=$HTTP_POST_VARS['year'];
       $rating=$HTTP_POST_VARS['rating'];
   
 // if any of the follwoing variables are empty do not insert into database
  if(!empty($albumId) and (!empty($albumTitle)) and  (!empty($artistBandName)) and  (!empty($genre)) and (!empty($year)) and (!empty($rating)) ) { 
         
	$query = "INSERT INTO Album(albumId,AlbumTitle,classicalTrack,cdNumber,artistBandName,genre,label,year,rating)
                  VALUES('$albumId','$albumTitle','$classicalTrack','$cdNumber','$artistBandName','$genre','$label','$year', '$rating')";   
		  
	  	 $result = mysql_query($query) or die  ("Execution failed.");
		 
		 }else {
		 
		 echo " Make sure all the fields are entered!";
		 
	}

?>	
	<form method="post"action="<?php echo $PHP_SELF?>">
	<table cellpadding=2 cellspacing=0 border=0>

	<td><font color="red">Album Id    :</font></td><td>       		<input type="text" size="5" maxlength="10"                  name="albumId"  value="<?echo $albumId; ?>"></td><tr>

      <td><font color="red">Album Title:</font></td><td>				<input type="text" size="30" maxlength="30"                      name="albumTitle" value="<?echo $albumTitle; ?>"></td></tr>

      <td>Classical Track:</td><td>			<input type="text" size="30" maxle2th="30"                      name="classicalTrack"value="<?echo $classicalTrack;?> "></td></tr>

	<td>Cd Number:(same as Album ID)</td><td>			        <input type="text" size="2" maxlength="2"                      name="cdNumber" value="<?echo $cdNumber;?>"></td></tr>

	<td><font color="red"> Artist / Band Name:</font></td><td>   			<input type="char" size="30" maxlength="30"                       name="artistBandName" value="<?echo $artistBandName;?>"> </td><tr>

	<td><font color="red"> Genre:(e.g pop, rock, R &B...)</font> </td><td>       			<input type="char" size="20" maxlength="20"                       name="genre" value="<?echo $genre;?>"></td><tr>


        <td>Label:</td><td>                          <input type="char" size="20" maxlength="20"                         name="label" value="<?echo $label;?>"></td><tr>

	<td><font color="red">Year  (e.g.yyyy-mm-dd):</font></td><td>  	        <input type="char" size="10" maxlength="10"                         name="year"value="<?echo $year;?>"></td><tr>

	<td><font color="red">Rating:(e.g use- bad, ok, good, great)</font></td><td>  			        <input type="char" size="10" maxlength="10"                name="rating" value="<?echo $rating;?>"></td><tr>

	</table>
	<input type="Submit" name="class" value="Enter Album Title Info.">
	<input type="Reset">
	
	</form>

  <br> </br>

	<p>You Can view the updated General Music Collection with information that you have entered <a href = "http://www.csc.liv.ac.uk/~u1phg/selectAlbum.php">  - Here!</a></p>
	
	<br> </br>
	<center><p>To submit or query the other collections  choose from below:</p></center> 
	
	<center><p> <a href = "http://www.csc.liv.ac.uk/~u1phg/IntroClassicalAlbum.php">Classical Music</a></p>  
	<p> <a href = "http://www.csc.liv.ac.uk/~u1phg/introDVD.php">DVDs</a></p>  
	<p> <a href = "http://www.csc.liv.ac.uk/~u1phg/introBookTitle.php">Books</a></p></center>
preetgowda
Forum Newbie
Posts: 7
Joined: Wed Mar 31, 2004 4:35 am

Post by preetgowda »

If any one could please give me some advice on the topic above that would be great!!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

break the date up and make a UNIX timestamp. You can then call date() on it to get the year. I think there is a date-type function that will allow you to check if a certain combination of year, month, day will create a valid date.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
preetgowda
Forum Newbie
Posts: 7
Joined: Wed Mar 31, 2004 4:35 am

Post by preetgowda »

Thank you, but i am really not sure what you mean? i do not understand the unox time stamp?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

The UNIX timestamp is the a way of identifying exactly what second it is. Calling the [php_man]time()[/php_man] function will give you the number of seconds since the UNIX epoch, 12 midnight on January 1, 1970. Since the timestamp is just a big integer, it's very easy to add. Plus, calling [php_man]date()[/php_man] on a timestamp can give you many useful pieces of information about that timestamp. Further, if you've got only a year, month and day, use [php_man]checkdate()[/php_man] to check if it's they constitute a valid date, and [php_man]mktime()[/php_man] to convert them to a timestamp.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply