I would tend to agree with you regarding the use of selects, for a public website. You can't trust the user to not be a thicko or, on the other hand, an american (
) and enter the date incorrectly. I like cups' dual method of using selects for those without js.
However what I'm working on right now is a back-office admin page so it means 1) I can rely a little more on the user 2) They're doing repetitive jobs so quick and easy is the way (ie not having a row of 3 selects).
But such usability talk is a bit off topic.
Regarding the parsing of dates. When I come down to it, all I'm doing is 2 things:
1. getting a datetime from the database and displaying it in some format
2. Getting some user-entered date and putting it in the database as a datetime
To this end I just came up with 2 pretty simple functions to do this:
Code:
function date_to_db($dateStr)
{
//split it
$ukDatePattern="/^([0-9]{1,2})[\/\.,\- ]([0-9]{1,2})[\/\.,\- ]([0-9]{2,4})( ([0-9]{1,2}) ?: ?([0-9]{1,2})( ?: ?([0-9]{1,2}))?)?$/";
if(!preg_match_all($ukDatePattern, $dateStr, $matches)) return false;
if(count($matches)<3) return false;
$d=$matches[1][0];
$m=$matches[2][0];
$y=$matches[3][0];
//check parts
if(!checkdate($m,$d,$y)) return false;
//stick it back together
$dbDate=$y."-".$m."-".$d;
//check for time elements
if($matches[5][0]!="")
{
$dbDate.=" ".$matches[5][0];
$dbDate.=":".$matches[6][0];
if($matches[8][0]!="")
{
$dbDate.=":".$matches[8][0];
}
}
return $dbDate;
}
function date_from_db($dateStr,$format="d/m/Y")
{
$stamp=strtotime($dateStr);
if($stamp==-1) return false;
return date($format,$stamp);
}
I was basically wanting to check I wasn't re-inventing the wheel. It's been a fun journey and we got there in the end. Cheers
Bookmarks