Who’d have thought that capturing dates in an online application could be so complex? Really? You capture the form using HTML5, process it server side, then dump it in to your database – SLAM DUNK! The only problem is that the ball has rebounded off of the hoop, and you need to rethink your entire play all the way back from the moment you got the ball!
Without teaching ‘granny to suck eggs’, the technologies used in this particular case are HTML5, PHP and MySql.
HTML is very straight forward, and so is PHP because all you need to do is line up your $_POST vars with the HTML5 input type vars:
<input name=”myDate” type =”date” />
Great, this gives you a date picker, with the format “d-m-Y”. Unless your locale is US in which case “m-d-Y”. This is great for IE. What about non-IE browsers? Simple, “d/m/Y” unless you have a US locale in which case “m/d/Y”. BUT don’t forget that non-IE browsers will not provide a date picker, so we will need a placeholder advising the user how to enter the date.
<input name=”myDate” type=”date” placeholder=”dd-mm-yyyy” />
So far good. PHP can still capture the date in any scenario:
$myVar = $_POST[‘myDate’];
Now here’s where the issue rebounds off the hoop! MySql will only accept dates in the form of “d-m-Y”, OR “m-d-Y”. So the parsing and checking needs to be done by PHP.
So here’s what it boiled down to in the end:
- Check for “/” in the date and replace them all with “-“;
- Reverse the date from “dd-mm-YYYY” to “YYYY-mm-dd”.
$_POST[‘Order_Date’] = str_replace(‘/’,’-‘,$_POST[‘Order_Date’]);
$_POST[‘Order_Date’] = date(‘Y-m-d’, strtotime($_POST[‘Order_Date’]));
I make it sound easy right! Well so far so good. This is the method I am sticking with for now!