Easiest way for user to enter date
Moderator: General Moderators
Easiest way for user to enter date
Hello, I've written all the php code for a date to be inserted into a new record into a mysql db. I've learned that the format needs to be yyyy-mm-dd. My question is if I have a form for the user to enter info, what's the easiest way for the user to enter the date and for me to get the date in the correct format that mysql requires? Is there a simple calendar function? I've never had to use a calendar on a web page. Everything works right now if I enter 2011-07-22, but I'd like to make it simplier.Thanks for your assistance!!
Re: Easiest way for user to enter date
Depending what local most of your visitors would be, (ie, if US, generally it is month day year), I would prompt them to enter it mm-dd-yyyy format. then use:
This will technically enter any of the following:
mm-dd-yyyy
m-d-yy
mm/dd/yyyy
m.d.yy
As long as it is in that order.
Code: Select all
<?php
$aryErr = array(); // list of validation errors to display back at top of the form
// use this if you want to make sure year is 4 digits
// if (!preg_match('/^(\d?\d)[^\d](\d?\d)[^\d]((\d{2})\d{2})$/',$_POST['txtDate'],$regs)) {
// use this if you don't care if they do 2 or 4 digit year
if (!preg_match('/^(\d?\d)[^\d](\d?\d)[^\d]((\d{2}?)\d{2})$/',$_POST['txtDate'],$regs)) {
$aryErr['Date'] = "Date must be in the format mm-dd-yyyy.";
}
else {
$intMonth = $regs[1];
$intDay = $regs[2];
$intYear = $regs[4]; // 3 = the 19 or 20 for 4 digit year
// do some other checking to see if it is in a range you want to accept
if (!$bGoodDateRange) {
$aryErr['Date'] = "Date is out of range please enter one (specify limits here)";
}
}
?>mm-dd-yyyy
m-d-yy
mm/dd/yyyy
m.d.yy
As long as it is in that order.
Re: Easiest way for user to enter date
Thanks for the code. I'll try it out!
Re: Easiest way for user to enter date
There are lots of ways you can do that. Think about web forms you have used--think of the good ones and the bad ones. In my opinion, I would rank them in the following order, from Easiest But Least User Friendly to Harder But Much Better, I would say:
- Next to your date input area, instruct the user what format they should enter.
- Provide 3 Select data inputs with dropdown lists, one each for month, day, and year.
- Use one of the literally hundreds of Javascript functions that displays a small calendar and lets the user pick the date. Search for javascript date picker