Get all records between two dates - codeigniter

How can i fetch all records between two date field dates in mysql table.Am using code igniter for my application.
Am entering start date and end date from date picker , need to fetch all rowas >= startdate and <= end date
my code:-
$this->db->where('start_date <= ',$start_date);
$this->db->where('end_date >= ',$end_date);
My query :- SELECT * FROM table WHERE start_date <= '2016-04-13' AND end_date >= '2016-04-19'
This is fetching only one row from table.
Please help

U need to do something like this...
$this->db->select("DATE_FORMAT(date, '%m/%d/%Y') as Urdate",FALSE);
$this->db->from('table');
$this->db->where("DATE_FORMAT(date,'%Y-%m-%d') > '2013-01-01'",NULL,FALSE);

Try this one:
$this->db->where('start_date >= ',$start_date);
$this->db->where('end_date <= ',$end_date);

$this->db->select('*');
$this->db->from('manual_discount');
$this->db->where("DATE_FORMAT(created_datetime,'%Y-%m-%d') >= '2018-09-06'",NULL,FALSE);
$this->db->where("DATE_FORMAT(created_datetime,'%Y-%m-%d') <= '2018-09-06'",NULL,FALSE);
this works fine for me

To solve the above issue you should following below query.
$this->db->select('*');
$this->db->from('table');
$this->db->group_start();
$this->db->where('start_date <=',$start_date);
$this->db->where('end_date >=',$start_date);
$this->db->group_end();
$this->db->group_start();
$this->db->where('start_date <=',$end_date);
$this->db->where('end_date >=',$end_date);
$this->db->group_end();

Related

How can i show single data in view if today date is in between start date & end date in laravel?

I have Admission table which have id, school_id, start_date,end_date etc and i want to show single data if today's date is between start_date & end_Date in laravel eloquent
Applying below code you can get the latest data of current date
$currentDate = date('Y-m-d');
$result = my_table::where(DB::raw('DATE(start_date)'), '<=' , $currentDate)
->where(DB::raw('DATE(end_Date)'), '>' , $currentDate)
->orderBy('id','desc')
->first();
`$start_date = date('2018-01-01');
$end_date = date('2018-05-02');
Adminssion::whereBetween(Carbon\Carbon::now()->toDateString(), [$start_date, $end_date])->get();`
The carbon now() function will give you current date and time. And toDateString() function only give you today date. And with whereBetween() function you can compare the current date between start_date and end_date.

symfony2 how to create query builder between two dates with startdate enddate included

How to create query builder between two dates (startdate enddate) with startDate endDate included.
SQL example : Select * from fiche.f
WHERER ((f.creationdate >= satrtDate)
AND (f.creationdate <= endDate))
In PostgreSQL, I would use BETWEEN $date1 and $date2 and it seems to work in Doctrine too.
$queryBuilder->where('f.creationdate BETWEEN f.startDate and f.endDate')
You can use $startdate = date('Y-m-d', strtotime($start. ' - 1 days')) and $enddate = date('Y-m-d', strtotime($end. ' + 1 days'))

Laravel eloquent possible bug?

I want to join two tables and filter on a field in the joined table. I don't think the actual tables matter in this question, but it's a table with dates joined with a table with the event info, so there are more dates possible for 1 event.
I made this eloquent line:
Event_date::whereRaw('startdate >= curdate() OR enddate >= curdate()')->whereHas('Event', function($q){$q->where("approved",true );})->orderBy('startdate', 'asc')->orderBy('enddate', 'asc')->toSql());
the filter doesn't work though. So thats why i added the ->toSql() to the line.
I get the following back:
select * from `event_dates` where startdate >= curdate() OR enddate >= curdate() and exists (select * from `events` where `event_dates`.`event_id` = `events`.`id` and `approved` = ?) order by `startdate` asc, `enddate` asc
You see that the 'where("approved",true )' results in 'where ..... and and approved = ?)' Where does the questionmark come from??? I tried diferent things, like '1', 1, 'True', True, true, 'true'...everything comes back as a questionmark.
Any suggestions??
Thanks!
Erwin
This is expected behaviour. Laravel uses prepared statements. To get parameters that are put into placeholders, you can use
$query->getBindings();
so for example in your case you can use:
$query = Event_date::whereRaw('startdate >= curdate() OR enddate >= curdate()')->whereHas('Event', function($q){$q->where("approved",true );})->orderBy('startdate', 'asc')->orderBy('enddate', 'asc'));
and now
echo $query->toSql();
var_dump($query->getBindings());
to get both query with placeholders and values that will be put in place of placeholders.

Delete using active record

How to achieve below query in ActiveRecord ?
"delete from model where date(created_at)=#{some_date}"
where created_at is sql datetime field.
One option I can think is calculate
start_date = starting time of the day
end_date = end time of the day
and
Model.delete_all('created_at' >= start_date, 'created_at' < end_date)
Any other clean option ?
Assuming database you are using is MySql and start_date is a date object.
Use mysql DATE_FORMAT function
Model.delete_all("DATE_FORMAT(created_at, '%Y-%m-%d') >= ?",
start_date.strftime("%Y-%m-%d"))

Date comparision

In LINQ, I write a query for data, which is between two dates. In this case I am not getting data. When user enters same date as from date and to date the data is not displaying while there are data for that particular date.
This is my LINQ query.
var result = from MaterialStatus in materialRequistionEntities.Tbl_MatReq_NewItemReq_M
join CurrentStatus in materialRequistionEntities.Tbl_StatusMaster_M
on MaterialStatus.ReqCurStatus equals CurrentStatus.StatusId
join Employee in materialRequistionEntities.Tbl_Emp_Master_M
on MaterialStatus.Indentor equals Employee.EmpId
where (MaterialStatus.CreatedOn >= dt.Date
&& MaterialStatus.CreatedOn <= dt1.Date)
select *****************
You should do:
var upperDate = dt1.Date.AddDays(1);
...
where (MaterialStatus.CreatedOn >= dt.Date
&& MaterialStatus.CreatedOn < upperDate
This way the expression can be converted to a sargable SQL predicate.
Assuming that dt1 is in reality greater that dt, you could use
var result = from MaterialStatus in materialRequistionEntities.Tbl_MatReq_NewItemReq_M
join CurrentStatus in materialRequistionEntities.Tbl_StatusMaster_M on
MaterialStatus.ReqCurStatus equals CurrentStatus.StatusId
join Employee in materialRequistionEntities.Tbl_Emp_Master_M
on MaterialStatus.Indentor equals Employee.EmpId
where (MaterialStatus.CreatedOn.Date >= dt.Date && MaterialStatus.CreatedOn.Date <= dt1.Date)
select *****************
Notice CreatedOn.Date to deal with the hour/minute/seconds/milliseconds components
I hope it will be ok.
where (MaterialStatus.CreatedOn.Value.Date >= dt.Date && MaterialStatus.CreatedOn.Value.Date <= dt1.Date.AddDay(1))

Resources