You could try something along these lines
<?php
function myDateFormat($d) {
switch (strlen("$d")) {
case 4: return "'%Y'";
case 6: return "'%Y%m'";
case 8: return "'%Y%m%d'";
}
}
// the date search string
$dateStr = "2004, 200501, 20050201, 20050701-20050731";
$dates = explode (',', $dateStr);
$where = array();
foreach ($dates as $d) {
$d = trim($d);
//is it a range
if (strpos($d,'-')!==false) {
list($d1, $d2) = explode ('-', $d);
$d1 = trim($d1);
$d2 = trim($d2);
$f = myDateFormat($d1);
$where[] = sprintf("(DATE_FORMAT(datecol, %s) BETWEEN '%d' AND '%d')", $f, $d1, $d2);
}
else {
$f = myDateFormat($d);
$where[] = "(DATE_FORMAT(datecol, $f) = '$d')";
}
}
$wheredate = '(' . join (" \nOR ", $where) . ')';
echo '<pre>', $wheredate, '</pre>';
?>
-->
((DATE_FORMAT(datecol, '%Y') = '2004')
OR (DATE_FORMAT(datecol, '%Y%m') = '200501')
OR (DATE_FORMAT(datecol, '%Y%m%d') = '20050201')
OR (DATE_FORMAT(datecol, '%Y%m%d') BETWEEN '20050701' AND '20050731'))