Here is a solution that might help.
You can make a common generalized getSearchDateRange function it will return you an array having search start date and end date and use these values in your query.
In this way you can use this search range function anywhere you want to use.
You can get the "From and To" date using $_POST.
function getSearchDateRange() {
Then get the date box value
$searchBeginDay = $_POST['search_begin_date'];
$searchBeginMonth = $_POST['search_begin_month'];
$searchBeginYear = $_POST['search_begin_year'];
$searchEndDay = $_POST['search_end_date'];
$searchEndMonth = $_POST['search_end_month'];
$searchEndYear = $_POST['search_end_year'];
$selectedOption = $_POST['mySelect'];
if($selectedOption == '') {
$date = date_create(date('Y-m-d'));
$searchRange = '90 days';
date_sub($date, date_interval_create_from_date_string($searchRange));
$searchStartDate = date_format($date, 'Y-m-d');
$searchBeginDay = 'dd';
$searchBeginMonth = 'mm';
$searchBeginYear = 'yyyy';
} else if($selectedOption == '30') {
$date = date_create(date('Y-m-d'));
$searchRange = '30 days';
date_sub($date, date_interval_create_from_date_string($searchRange));
$searchStartDate = date_format($date, 'Y-m-d');
$searchBeginDay = 'dd';
$searchBeginMonth = 'mm';
$searchBeginYear = 'yyyy';
} else if($selectedOption == '3') {
$thisYear = date('Y');
$lastYear = $thisYear - 1;
$searchStartDate = date($lastYear."-01-01");
$searchBeginDay = 'dd';
$searchBeginMonth = 'mm';
$searchBeginYear = 'yyyy';
}else {
}
if($selectedOption == '3') {
$thisYear = date('Y');
$lastYear = $thisYear - 1;
$searchEndDate = date($lastYear."-12-31");
$searchEndDay = 'dd';
$searchEndMonth = 'mm';
$searchEndYear = 'yyyy';
}else {
if(is_numeric($searchEndDay)&& is_numeric($searchEndMonth) && is_numeric($searchEndYear)) {
$searchEndDate = $searchEndYear.'-'.$searchEndMonth.'-'.$searchEndDay;
}else{
$date = date_create(date('Y-m-d'));
$searchEndDate = date_format($date, 'Y-m-d');
$searchEndDay = 'dd';
$searchEndMonth = 'mm';
$searchEndYear = 'yyyy';
}
}
$searchDateRange['startDate'] = $searchStartDate;
$searchDateRange['endDate'] = $searchEndDate;
return $searchDateRange;
}
By calling this function you get start and end date for search .So no need to write the query again and again.
$getDateData = $db->fetchOne("select count(id) as total_users from users where created_at > $searchDateRange['startDate'] AND created_at < $searchDateRange['endDate']");