Jump to content

Removing 30th and 31st from february in Date Selection List


terungwa
Go to solution Solved by Barand,

Recommended Posts

My date selection list below allows selection of 30th, 31st and 29th february (on non-leap year). Although I noticed the mysql database is treating those entries as invalid and records them as 0000-00-00.

 

How may i remove this from the user input interface?

<?php
 $monthName = array(1 => "January", "February", "March",
                         "April", "May", "June", "July",
                         "August", "September", "October",
                         "November", "December");
 $today = time();                   //stores today's date
 $f_today = date("M-d-Y",$today);   //formats today's date
$startyear = date("Y")-60;
 echo "<div style = 'text-align: center'>\n";
 echo "<form action='$_SERVER[PHP_SELF]' method='POST'>\n";

 /* build selection list for the month */
 $todayMO = date("n",$today);  //get the month from $today
 echo "<select name='month'>\n";
 for ($n=1;$n<=12;$n++) 
 {
   echo " <option value=$n";
   if ($todayMO == $n)
   {
     echo " selected";
   }
   echo " > $monthName[$n]\n\n</option>";
 }
 echo "</select>\n";

 /* build selection list for the day */
 $todayDay= date("d",$today);    //get the day from $today
 echo "<select name='day'>\n";
 for ($n=1;$n<=31;$n++)  
 {
   echo " <option value=$n";
   if ($todayDay == $n ) 
   {
     echo " selected";
   }
   echo " > $n</option>\n";
 }
 echo "</select>\n";

 /* build selection list for the year */
 echo "<select name='year'>\n";
 for ($n=$startyear;$n<=$startyear+200;$n++)
 {
   echo " <option value=$n";
   if ($startyear == $n )
   {
     echo " selected";
   }
   echo " > $n</option>\n";
 }
 echo "</select>\n";
 echo "<p>
<input type=\"submit\" name=\"insert\" value=\"Insert New Entry\" id=\"insert\">
</p></form></div>\n";
?>

Thanks.

Link to comment
Share on other sites

Currently, I am  subjecting the insert query to the PHP function
checkdate(), which is smart enough to know when its a leap year and prevents mistakes
such as September 31. but again, If i can remove it from the front-end, the better.

if (!checkdate($month,$day,$year)) {
    echo  'You have used an invalid date';
  } else{     
  // create SQL
  $sql = 'INSERT INTO date (
			date_value
			)
		VALUES(?)';
  if ($stmt->prepare($sql)) {
	// bind parameters and execute statement
	$stmt->bind_param('s', $date_value);
    // execute and get number of affected rows
	$stmt->execute();
	if ($stmt->affected_rows > 0) {
	  $OK = true;
	}
  }
  // redirect if successful or display error
  if ($OK) {
	header('Location: insert_success.php');
	exit;
  } else {
	$error = $stmt->error;
  }
}}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.