I am trying to learn about managing dates on mysql, and I have been given an exercise to practise it.
On an input, the user must add a date formated as dd-mm-YYYY, and then, if the date is valid, I record in on the database, changing the format to YYYY-mm-dd as it is how DATE variable types are stored (as I understood), but, I haven't arrived yet...
This is what I have so far:
html
<form method="POST" action="testing.php">
<div>
<label for="title" class="required">Title</label>
<input name="title" id="title" type="text" value="" />
</div>
<div>
<label for="date" class="required">Date</label>
<input name="date" id="date" type="date" value="" />
</div>
<button class="submit" type="submit" name="submit">Submit</button>
</form>
php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$title = filter_input(INPUT_POST, 'title', FILTER_SANITIZE_STRING);
$string_input_date = filter_input(INPUT_POST, 'date', FILTER_SANITIZE_STRING);
$array_input_date = explode("-", $string_input_date);
if(!checkdate($array_input_date[1], $array_input_date[0], $array_input_date[2])) {
if(checkdate(!$array_input_date[1], $array_input_date[0], $array_input_date[2])) {
$error_message = "wrong month"; die($error_message);
} elseif (checkdate($array_input_date[1], !$array_input_date[0], $array_input_date[2])) {
$error_message = "wrong day";
} elseif (checkdate($array_input_date[1], $array_input_date[0], !$array_input_date[2])) {
$error_message = "wrong year";
} else {
$error_message = "Introduce a valid date";
}
} else {
//success! it is a valid date
}
}
I can see, it isn't working, but I run out of options about how can I check each argument one per one... The objective is, I have to display an error message if it is the day what is wrong, or the month, or the year.
At the moment, I try to introduce 35-04-2016 which should display the error message "wrong day", but instead it displays "Introduce a valid date". I know it is displaying that because my if-elseif are wrong but... any suggestion about how to check each argument separately?
P.S.: I know I could do it with regular expressions, or just creating myself rules like a month is between 1-12 etc etc, but my mentor says I have to use php features and built in functions.... so I CAN'T solve it creating my own rules or with regular expressions (for now, I guess if he sees me crying he might tell me to do whatever I want ^^)
Thank you