$sql = 'some sql statement'; //start a query
$where = 'WHERE '; //start a where clause
if ($day) { $where .= 'day='.$day; //if day is set... append day=value
if ($month) {$where .= (($day) ? ' and' : '') . ' month='.$month; //if month set and day set, append ' and month=value' otherwise just append month=value
if ($year) { $where .= (($day || $month) ? ' and' : '') . ' year='.$year; //if day or month is set append ' and year=value' otherwise append just 'year=value'
$sql .= $where; //combine the where clause with the sql query
$result = mysql_query($sql); //make the query happen
the (($day) ? ' and' : '') is an alternate form of an if statement that fits inline really nicely with variable strings....
basically this is if ($day) insert into this string ' and' else insert into this string ''