How to achieve if else statement while querying with database?
I'm not sure if you can add if statement to a query but as you already have the values of $form_state['values']['fromdate'] and $form_state['values']['todate'] you can just add an if statement. If those two don't have values then execute a different query without the condition('submitted_time',  array($from_date, $to_date), 'BETWEEN') condition.