I am trying to parse user-entered string dates with PHP. I need to remove all characters other than these two acceptable categories:
1) [0-9,\./-] (numerals, comma, period, slash, and dash)
2) An array of acceptable words:
$monthNames=array(
"january"=>1,
"jan"=>1,
"february"=>2,
"feb"=>2
);
I tried explode()ing on character word bounaries and then removing each section that is not in the array, but that led to quite a mess. Is there an elegant way of accomplishing this?
Thanks!
You could use
strtotime()To check for failure:
See http://php.net/manual/en/function.strtotime.php
Also
DateTime::createFromFormat()might be useful.See http://www.php.net/manual/en/datetime.createfromformat.php