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;
}