BTW, when the user type the word ‘today’, it will automatically refer to the current date in the same format and I got it working, now I’m having trouble for combining the validation above. here is my code
<?php
# Fetch the date from the post array, or default to today
$date = isset($_POST['date']) ? $_POST['date'] : 'today';
# Convert the provided date to a time stamp
$timestamp = strtotime($date);
# If the provided date could not be converted to a time stamp, default to the current date & time
if(empty($timestamp))
{
$timestamp = time();
}
# Format the timestamp to your desired date format
$formatted_date = date("Y-m-d", $timestamp);
?>
If you want to validate the date, convert it into UNIX value using strtotime() and then calculate if it’s between the two desired range dates. If it is, then convert it from the UNIX code into the format you want to store in your database. This way, you’re guaranteed to have the same date format as a result every time.
As JV started:
<?php
# Fetch the date from the post array, or default to today
$date = isset($_POST['date']) ? $_POST['date'] : 'today';
# Convert the provided date to a time stamp
$timestamp = strtotime($date);
# If the provided date could not be converted to a time stamp, default to the current date & time
if(empty($timestamp))
{
$timestamp = time();
} else {
if( $timestamp > strtotime('18 years ago') ){
echo "Sorry, but you must be 18 years old to view this website";
exit;
}
}
# Format the timestamp to your desired date format
$formatted_date = date("Y-m-d", $timestamp);
?>
If you’re real paranoid, make sure the leading zero is attached to month and day - though you can make this part of the value of the options in the select as shown above.
if (strlen($_POST['date']['month']) == 1) {
$_POST['date']['month'] = '0'.$_POST['date']['month'];
}
And the same code for the day. Note that the only way this correction actually executes if someone is submitting to your page with their own form - a normal user doesn’t face this problem. This does protect the database from bad inputs.
While select boxes feel clumsy if you’re on keyboard and mouse - they are easier to deal with. Even easier is the date picker html 5 tool - but at the moment only Opera implements this.
I’m making this whole thing for firefox users. I’m using opera by default, I’m just trying to validate the input in firefox just in case there will be a wrong input from other browser when somebody make a pun of my system then I can guard my db from that.
In that event run a javascript that emulates HTML 5 inputs for most browsers (Opera alone supports input type=“date” at the moment). You’ll can split the string on ‘-’ and then pass the three resulting numbers to checkdate as in my example above. The goal remains the same - avoid arbritrary input from users when possible.
It replaces it in a manner that will work across all browsers. You cannot safely use <input type=“date”> because the majority of browsers do not support webforms 2.0. If you do want to use them you must use javascript extensions which emulate the behaviors of those objects.