Inserting Date from web form

I have tried checking through the web how to insert date from dropdown menu into mysql but couldn’t get it done. I don’t want to use unix timestamp as it is limited in range because I want insert date of birth of those born earlier than 1970. Also, some form might have to contain more than one date, for example my search form has two dates to specify the range for the information to be retrieved from the database. I don’t want to use unix timestamp please. My present code is below and I write it like that so that when I want to edit info from the database, whatever I have stored will be displayed first. Any advice and example of how to get this done will be highly appreciated. I’m providing the code for my test page below but please note that I can’t get the date into the database yet but I was able to do that using unix timestamp.

My form is below:

<?php
require_once $_SERVER['DOCUMENT_ROOT'] . 'classes/DatabaseManager.php';
$conn = DatabaseManager::getConnection();

  require_once('utility_funcs.inc.php');

  $purchaseDate = convertDateToMySQL($_POST['month'], $_POST['day'], $_POST['year']);

if(isset($_POST['convert']) && $_POST['convert'] == 'Convert')
{
	$sql = "INSERT INTO mydate (purchaseDate, firstname) VALUES (:purchaseDate, :firstname)";
	try
	{
		$st = $conn->prepare ( $sql );
		$st->bindParam( ":purchaseDate", $_POST['purchaseDate'], PDO::PARAM_INT );
		$st->bindParam( ":firstname", $_POST['firstname'], PDO::PARAM_STR );
		$st->execute();
		$this->id = $conn->lastInsertId();
		$conn = null;
	}
	catch(PDOException $e)
	{
		echo 'Unable to insert purchase date' . $e->getMessage();
		exit();
	}
}
		
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Convert Date to MySQL Format</title>
<style>
input[type="number"] {
	width:50px;
}
</style>
</head>

<body>
<form id="form1" method="post" action="">
  <p>
    <label for="select">Month:</label>
        <select name="month" id="month">
<option value="">Select</option>

<option value="1" <?php if(isset($month) && $month == '1') echo 'selected';  ?> >Jan</option>
<option value="2" <?php if(isset($month) && $month == '2') echo 'selected';  ?> >Feb</option>
<option value="3" <?php if(isset($month) && $month == '3') echo 'selected';  ?> >Mar</option>
<option value="4" <?php if(isset($month) && $month == '4') echo 'selected';  ?> >Apr</option>
<option value="5" <?php if(isset($month) && $month == '5') echo 'selected';  ?> >May</option>
<option value="6" <?php if(isset($month) && $month == '6') echo 'selected';  ?> >Jun</option>
<option value="7" <?php if(isset($month) && $month == '7') echo 'selected';  ?> >Jul</option>
<option value="8" <?php if(isset($month) && $month == '8') echo 'selected';  ?> >Aug</option>
<option value="9" <?php if(isset($month) && $month == '9') echo 'selected';  ?> >Sep</option>
<option value="10" <?php if(isset($month) && $month == '10') echo 'selected';  ?> >Oct</option>
<option value="11" <?php if(isset($month) && $month == '11') echo 'selected';  ?> >Nov</option>
<option value="12" <?php if(isset($month) && $month == '12') echo 'selected';  ?> >Dec</option>
        </select>
    <label for="day">Date:</label>
    <select name="day">
    <option value="">Select</option>
<option value="1" <?php if(isset($day) && $day == '1') echo 'selected';  ?> >1</option>
<option value="2" <?php if(isset($day) && $day == '2') echo 'selected';  ?> >2</option>
<option value="3" <?php if(isset($day) && $day == '3') echo 'selected';  ?> >3</option>
<option value="4" <?php if(isset($day) && $day == '4') echo 'selected';  ?> >4</option>
<option value="5" <?php if(isset($day) && $day == '5') echo 'selected';  ?> >5</option>
<option value="6" <?php if(isset($day) && $day == '6') echo 'selected';  ?> >6</option>
<option value="7" <?php if(isset($day) && $day == '8') echo 'selected';  ?> >7</option>
<option value="8" <?php if(isset($day) && $day == '8') echo 'selected';  ?> >8</option>
<option value="9" <?php if(isset($day) && $day == '9') echo 'selected';  ?> >9</option>
<option value="10" <?php if(isset($day) && $day == '10') echo 'selected';  ?> >10</option>
<option value="11" <?php if(isset($day) && $day == '11') echo 'selected';  ?> >11</option>
<option value="12" <?php if(isset($day) && $day == '12') echo 'selected';  ?> >12</option>
<option value="13" <?php if(isset($day) && $day == '13') echo 'selected';  ?> >13</option>
<option value="14" <?php if(isset($day) && $day == '14') echo 'selected';  ?> >14</option>
<option value="15" <?php if(isset($day) && $day == '15') echo 'selected';  ?> >15</option>
<option value="16" <?php if(isset($day) && $day == '16') echo 'selected';  ?> >16</option>
<option value="17" <?php if(isset($day) && $day == '17') echo 'selected';  ?> >17</option>
<option value="18" <?php if(isset($day) && $day == '18') echo 'selected';  ?> >18</option>
<option value="19" <?php if(isset($day) && $day == '19') echo 'selected';  ?> >19</option>
<option value="20" <?php if(isset($day) && $day == '20') echo 'selected';  ?> >20</option>
<option value="21" <?php if(isset($day) && $day == '21') echo 'selected';  ?> >21</option>
<option value="22" <?php if(isset($day) && $day == '22') echo 'selected';  ?> >22</option>
<option value="23" <?php if(isset($day) && $day == '23') echo 'selected';  ?> >23</option>
<option value="24" <?php if(isset($day) && $day == '24') echo 'selected';  ?> >24</option>
<option value="25" <?php if(isset($day) && $day == '25') echo 'selected';  ?> >25</option>
<option value="26" <?php if(isset($day) && $day == '26') echo 'selected';  ?> >26</option>
<option value="27" <?php if(isset($day) && $day == '27') echo 'selected';  ?> >27</option>
<option value="28" <?php if(isset($day) && $day == '28') echo 'selected';  ?> >28</option>
<option value="29" <?php if(isset($day) && $day == '29') echo 'selected';  ?> >29</option>
<option value="30" <?php if(isset($day) && $day == '30') echo 'selected';  ?> >30</option>
<option value="31" <?php if(isset($day) && $day == '31') echo 'selected';  ?> >31</option>
</select>
    <label for="year">Year:</label>
    <input name="year" type="number" required id="year" maxlength="4" placeholder="YYYY" value="<?php if(isset($year)) echo $year; ?>">
  </p>

  <p><label for="firstname">Firstname:</label>
  <input name="firstname" type="text" />

  <p>
    <input type="submit" name="convert" id="convert" value="Convert">
  </p>
</form>

</body>
</html>

The utility function included above is presented here

<?php
function convertDateToMySQL($month, $day, $year) {
  $month = trim($month);
  $day = trim($day);
  $year = trim($year);
  $result[0] = false;
  if (empty($month) || empty($day) || empty($year)) {
	$result[1] = 'Please fill in all fields';
  } elseif (!is_numeric($month) || !is_numeric($day) || !is_numeric($year)) {
    $result[1] = 'Please use numbers only';
  } elseif (($month < 1 || $month > 12) || ($day < 1 || $day > 31) || ($year < 1000 || $year > 9999)) {
	$result[1] = 'Please use numbers within the correct range';
  } elseif (!checkdate($month,$day,$year)) {
    $result[1] = 'You have used an invalid date';
  } else {
    $result[0] = true;
    $result[1] = "$year-$month-$day";
  }
  return $result;
}
$st->bindParam( ":purchaseDate", [COLOR="#FF0000"]$_POST['purchaseDate'][/COLOR], PDO::PARAM_INT );

You’re not using your converted result here.

Use [fphp]range[/fphp] to generate the list of dates, if indeed you must do it this way.

Replace all of this:


<select name="day">
    <option value="">Select</option>
<option value="1" <?php if(isset($day) && $day == '1') echo 'selected';  ?> >1</option>
<option value="2" <?php if(isset($day) && $day == '2') echo 'selected';  ?> >2</option>
<option value="3" <?php if(isset($day) && $day == '3') echo 'selected';  ?> >3</option>
<option value="4" <?php if(isset($day) && $day == '4') echo 'selected';  ?> >4</option>
<option value="5" <?php if(isset($day) && $day == '5') echo 'selected';  ?> >5</option>
<option value="6" <?php if(isset($day) && $day == '6') echo 'selected';  ?> >6</option>
<option value="7" <?php if(isset($day) && $day == '8') echo 'selected';  ?> >7</option>
<option value="8" <?php if(isset($day) && $day == '8') echo 'selected';  ?> >8</option>
// etc ...

With something like this:


$dates = range(1,31);  // create an array of numbers 1-31
$match = 3 ;

echo "<select id=day name=day >" . PHP_EOL ;

foreach ($dates as $date){  // iterate (loop) through the array

  $selected = ($date === $match) ? "selected=selected" : "" ;
   // same as writing : if this date matches the chosen date, make it selected else echo empty string

  echo "<option = $date $selected>$date</option>" . PHP_EOL ;
}

echo "</select>" . PHP_EOL ;

As it stands that function returns an array with 2 items in it, either false and an error message or true and a date.

You would test for true and insert the data like this:

As StarLion suggests use your newly computed value, but watch out it is an array.


$purchaseDate = convertDateToMySQL($_POST['month'], $_POST['day'], $_POST['year']);

// $purchaseDate is now an array

// temp line of debug
var_dump($purchaseDate) ; // inspect it

if($purchaseDate[0] === true){
// go ahead and try and insert it into the db


// etc

$st->bindParam( ":purchaseDate", $purchaseDate[1]);

// etc

}else{

echo "Warning: $purchaseDate[1]" ;

}

StarLion and Cups, you guys are just too good. I have been able to insert the date into my database now through the codes you guys provided for me. Thanks a lot but one more thing. In case I have purchase date, birth date, wedding date on a single form, how do i address this because as you know, all of these makes use of date, month and year in common. How do i differentiate one from the other so that the right date for each is entered into the database? Thanks in advance.

You would have to show us the form which generates these three different dates, as to the PMs which you sent me, you should post the content as a new thread on this forum.

The form is just below. I understand that my major challenge here is how to differentiate each date from each other since they all have date, month and year in common. What do you suggest I do because the utility function I called checks the validity of the dates.

<?php
require_once $_SERVER['DOCUMENT_ROOT'] . 'classes/DatabaseManager.php';
$conn = DatabaseManager::getConnection();

  require_once('utility_funcs.inc.php');
  
  $dateOfBirth = convertDateToMySQL($_POST['month'], $_POST['day'], $_POST['year']);
  $weddingAnniversaryDate = convertDateToMySQL($_POST['month'], $_POST['day'], $_POST['year']);
  $wifeDateOfBirth = convertDateToMySQL($_POST['month'], $_POST['day'], $_POST['year']);

  // $purchaseDate is now an array

  // temp line of debug
  // var_dump($purchaseDate) ; // inspect it

  
if(isset($_POST['submit']) && $_POST['submit'] == 'Submit')
{
    $sql = "INSERT INTO mydate (firstName, lastName, dateOfBirth, weddingAnniversaryDate, wifeDateOfBirth) VALUES firstName, :lastName, :dateOfBirth, :weddingAnniversaryDate, :wifeDateOfBirth)";
    try
    {
       $st->bindParam( ":firstname", $_POST['firstname'], PDO::PARAM_STR );
       $st->bindParam( ":lastname", $_POST['lastname'], PDO::PARAM_STR );
       if($dateOfBirth[0] === true){
          // go ahead and try and insert it into the db
             $st = $conn->prepare ( $sql );
             $st->bindParam( ":dateOfBirth", $dateOfBirth[1]); 
          }
          else
          {
              echo "Warning: $dateOfBirth[1]" ;
              }
       if($weddingAnniversaryDate[0] === true){
          // go ahead and try and insert it into the db
             $st = $conn->prepare ( $sql );
             $st->bindParam( ":weddingAnniversaryDate", $weddingAnniversaryDate[1]); 
          }
          else
          {
              echo "Warning: $weddingAnniversaryDate[1]" ;
              }
        
        if($wifeDateOfBirth[0] === true){
          // go ahead and try and insert it into the db
             $st = $conn->prepare ( $sql );
             $st->bindParam( ":wifeDateOfBirth", $wifeDateOfBirth[1]); 
          }
          else
          {
              echo "Warning: $wifeDateOfBirth[1]" ;
              }

        $st->execute();
        $conn = null;
    }
    catch(PDOException $e)
    {
        echo 'Unable to insert info into the database' . $e->getMessage();
        exit();
    }
}
        
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<table width="600" border="0" align="center" cellpadding="2" cellspacing="5">
<form id="form1" method="post" action="">
  <tr>
    <td width="30%" align="right" valign="middle">Lastname:

The form is just below. I understand that my major challenge here is how to differentiate each date from each other since they all have date, month and year in common. What do you suggest I do because the utility function I called checks the validity of the dates

<?php
require_once $_SERVER[‘DOCUMENT_ROOT’] . ‘classes/DatabaseManager.php’;
$conn = DatabaseManager::getConnection();

require_once(‘utility_funcs.inc.php’);

$dateOfBirth = convertDateToMySQL($_POST[‘month’], $_POST[‘day’], $_POST[‘year’]);
$weddingAnniversaryDate = convertDateToMySQL($_POST[‘month’], $_POST[‘day’], $_POST[‘year’]);
$wifeDateOfBirth = convertDateToMySQL($_POST[‘month’], $_POST[‘day’], $_POST[‘year’]);

// $purchaseDate is now an array

// temp line of debug
// var_dump($purchaseDate) ; // inspect it

if(isset($_POST[‘submit’]) && $_POST[‘submit’] == ‘Submit’)
{
$sql = “INSERT INTO mydate (firstName, lastName, dateOfBirth, weddingAnniversaryDate, wifeDateOfBirth) VALUES (:firstName, :lastName, :dateOfBirth, :weddingAnniversaryDate, :wifeDateOfBirth)”;
try
{
$st->bindParam( “:firstname”, $_POST[‘firstname’], PDO::PARAM_STR );
$st->bindParam( “:lastname”, $_POST[‘lastname’], PDO::PARAM_STR );
if($dateOfBirth[0] === true){
// go ahead and try and insert it into the db
$st = $conn->prepare ( $sql );
$st->bindParam( “:dateOfBirth”, $dateOfBirth[1]);
}
else
{
echo “Warning: $dateOfBirth[1]” ;
}
if($weddingAnniversaryDate[0] === true){
// go ahead and try and insert it into the db
$st = $conn->prepare ( $sql );
$st->bindParam( “:weddingAnniversaryDate”, $weddingAnniversaryDate[1]);
}
else
{
echo “Warning: $weddingAnniversaryDate[1]” ;
}

	if($wifeDateOfBirth[0] === true){
      // go ahead and try and insert it into the db
         $st = $conn-&gt;prepare ( $sql );
         $st-&gt;bindParam( ":wifeDateOfBirth", $wifeDateOfBirth[1]); 
      }
	  else
	  {
		  echo "Warning: $wifeDateOfBirth[1]" ;
		  }

	$st-&gt;execute();
	$conn = null;
}
catch(PDOException $e)
{
	echo 'Unable to insert info into the database' . $e-&gt;getMessage();
	exit();
}

}

?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=“Content-Type” content=“text/html; charset=utf-8” />
<title>Untitled Document</title>
</head>

<body>

<table width=“600” border=“0” align=“center” cellpadding=“2” cellspacing=“5”>
<form id=“form1” method=“post” action=“”>
<tr>
<td width=“30%” align=“right” valign=“middle”>Lastname:</td>
<td width=“70%”><input name=“lastName” type=“text” id=“lastName” /></td>
</tr>
<tr>
<td width=“30%” align=“right” valign=“middle”>Firstname:</td>
<td width=“70%”><input name=“firstName” type=“text” id=“firstName” /></td>
</tr>
<tr>
<td width=“30%” align=“right” valign=“middle”>Date of Birth:</td>
<td width=“70%”>
<label for=“select”>Month:</label>
<select name=“month” id=“month”>
<option value=“”>Select</option>

<option value=“1” <?php if(isset($month) && $month == ‘1’) echo ‘selected’; ?> >Jan</option>
<option value=“2” <?php if(isset($month) && $month == ‘2’) echo ‘selected’; ?> >Feb</option>
<option value=“3” <?php if(isset($month) && $month == ‘3’) echo ‘selected’; ?> >Mar</option>
<option value=“4” <?php if(isset($month) && $month == ‘4’) echo ‘selected’; ?> >Apr</option>
<option value=“5” <?php if(isset($month) && $month == ‘5’) echo ‘selected’; ?> >May</option>
<option value=“6” <?php if(isset($month) && $month == ‘6’) echo ‘selected’; ?> >Jun</option>
<option value=“7” <?php if(isset($month) && $month == ‘7’) echo ‘selected’; ?> >Jul</option>
<option value=“8” <?php if(isset($month) && $month == ‘8’) echo ‘selected’; ?> >Aug</option>
<option value=“9” <?php if(isset($month) && $month == ‘9’) echo ‘selected’; ?> >Sep</option>
<option value=“10” <?php if(isset($month) && $month == ‘10’) echo ‘selected’; ?> >Oct</option>
<option value=“11” <?php if(isset($month) && $month == ‘11’) echo ‘selected’; ?> >Nov</option>
<option value=“12” <?php if(isset($month) && $month == ‘12’) echo ‘selected’; ?> >Dec</option>
</select>
<label for=“day”>Date:</label>
<?php
$dates = range(1,31); // create an array of numbers 1-31
$match = 3 ;

echo “<select id=day name=day >” . PHP_EOL ;

foreach ($dates as $date){ // iterate (loop) through the array

$selected = ($date === $match) ? “selected=selected” : “” ;
// same as writing : if this date matches the chosen date, make it selected else echo empty string

echo “<option = $date $selected>$date</option>” . PHP_EOL ;
}

echo “</select>” . PHP_EOL ;

?>
<label for=“year”>Year:</label>
<input name=“year” type=“number” required id=“year” maxlength=“4” placeholder=“YYYY” value=“<?php if(isset($year)) echo $year; ?>”></td>
</tr>
<tr>
<td width=“30%” align=“right” valign=“middle”>Wedding Anniversary Date:</td>
<td width=“70%”>
<label for=“select”>Month:</label>
<select name=“month” id=“month”>
<option value=“”>Select</option>

<option value=“1” <?php if(isset($month) && $month == ‘1’) echo ‘selected’; ?> >Jan</option>
<option value=“2” <?php if(isset($month) && $month == ‘2’) echo ‘selected’; ?> >Feb</option>
<option value=“3” <?php if(isset($month) && $month == ‘3’) echo ‘selected’; ?> >Mar</option>
<option value=“4” <?php if(isset($month) && $month == ‘4’) echo ‘selected’; ?> >Apr</option>
<option value=“5” <?php if(isset($month) && $month == ‘5’) echo ‘selected’; ?> >May</option>
<option value=“6” <?php if(isset($month) && $month == ‘6’) echo ‘selected’; ?> >Jun</option>
<option value=“7” <?php if(isset($month) && $month == ‘7’) echo ‘selected’; ?> >Jul</option>
<option value=“8” <?php if(isset($month) && $month == ‘8’) echo ‘selected’; ?> >Aug</option>
<option value=“9” <?php if(isset($month) && $month == ‘9’) echo ‘selected’; ?> >Sep</option>
<option value=“10” <?php if(isset($month) && $month == ‘10’) echo ‘selected’; ?> >Oct</option>
<option value=“11” <?php if(isset($month) && $month == ‘11’) echo ‘selected’; ?> >Nov</option>
<option value=“12” <?php if(isset($month) && $month == ‘12’) echo ‘selected’; ?> >Dec</option>
</select>
<label for=“day”>Date:</label>
<?php
$dates = range(1,31); // create an array of numbers 1-31
$match = 3 ;

echo “<select id=day name=day >” . PHP_EOL ;

foreach ($dates as $date){ // iterate (loop) through the array

$selected = ($date === $match) ? “selected=selected” : “” ;
// same as writing : if this date matches the chosen date, make it selected else echo empty string

echo “<option = $date $selected>$date</option>” . PHP_EOL ;
}

echo “</select>” . PHP_EOL ;

?>
<label for=“year”>Year:</label>
<input name=“year” type=“number” required id=“year” maxlength=“4” placeholder=“YYYY” value=“<?php if(isset($year)) echo $year; ?>”></td>
</tr>
<tr>
<td width=“30%” align=“right” valign=“middle”>Wife’s Date of Birth:</td>
<td width=“70%”>
<label for=“select”>Month:</label>
<select name=“month” id=“month”>
<option value=“”>Select</option>

<option value=“1” <?php if(isset($month) && $month == ‘1’) echo ‘selected’; ?> >Jan</option>
<option value=“2” <?php if(isset($month) && $month == ‘2’) echo ‘selected’; ?> >Feb</option>
<option value=“3” <?php if(isset($month) && $month == ‘3’) echo ‘selected’; ?> >Mar</option>
<option value=“4” <?php if(isset($month) && $month == ‘4’) echo ‘selected’; ?> >Apr</option>
<option value=“5” <?php if(isset($month) && $month == ‘5’) echo ‘selected’; ?> >May</option>
<option value=“6” <?php if(isset($month) && $month == ‘6’) echo ‘selected’; ?> >Jun</option>
<option value=“7” <?php if(isset($month) && $month == ‘7’) echo ‘selected’; ?> >Jul</option>
<option value=“8” <?php if(isset($month) && $month == ‘8’) echo ‘selected’; ?> >Aug</option>
<option value=“9” <?php if(isset($month) && $month == ‘9’) echo ‘selected’; ?> >Sep</option>
<option value=“10” <?php if(isset($month) && $month == ‘10’) echo ‘selected’; ?> >Oct</option>
<option value=“11” <?php if(isset($month) && $month == ‘11’) echo ‘selected’; ?> >Nov</option>
<option value=“12” <?php if(isset($month) && $month == ‘12’) echo ‘selected’; ?> >Dec</option>
</select>
<label for=“day”>Date:</label>
<?php
$dates = range(1,31); // create an array of numbers 1-31
$match = 3 ;

echo “<select id=day name=day >” . PHP_EOL ;

foreach ($dates as $date){ // iterate (loop) through the array

$selected = ($date === $match) ? “selected=selected” : “” ;
// same as writing : if this date matches the chosen date, make it selected else echo empty string

echo “<option = $date $selected>$date</option>” . PHP_EOL ;
}

echo “</select>” . PHP_EOL ;

?>
<label for=“year”>Year:</label>
<input name=“year” type=“number” required id=“year” maxlength=“4” placeholder=“YYYY” value=“<?php if(isset($year)) echo $year; ?>”></td>
</tr>
<tr>
<td colspan=“2” align=“center” valign=“middle”><input type=“submit” name=“submit” id=“submit” value=“Submit” /></td>
</tr>
</form>
</table>

</body>
</html>