Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jeevamugunthan's avatar

how to get data from database where current date is in between from and to date

my code is

$current_date=date('Y-m-d');

            $maintenance=Maintenance::get()->where('admin_id', '=', $admin_id)->whereBetween($current_date, ['due_from', 'due_to'])->toArray();

but i get an empty array value

any body know how to fetch this

0 likes
17 replies
deansatch's avatar

try this:

$current_date=date('Y-m-d');

            $maintenance=Maintenance::where('admin_id',  $admin_id)->whereBetween($current_date, ['due_from', 'due_to'])->get()->toArray();

jeevamugunthan's avatar

@deansatch i tried this but i got error

Call to undefined method Illuminate\Database\Eloquent\Builder::toArray()
deansatch's avatar

double check it as I missed off the ->get() and you might have copied/pasted before I updated my answer

jeevamugunthan's avatar

@deansatch haahaa ok fine after copied your last answer again i got error

SQLSTATE[42S22]: Column not found: 1054 Unknown column '2020-03-23' in 'where clause' (SQL: select * from `maintanance` where `admin_id` = 11 and `2020-03-23` between due_from and due_to)

its assume current date as column

deansatch's avatar

oh...sorry - I spotted the get issue on your original query coming before your rules and didn't think to check the rest. The format of whereBetween is 'column, [ date1, date 2]

so

->whereBetween('due_from', [$current_date, $another_date])

deansatch's avatar

So I am guessing you are looking for something more like this:


$current_date=date('Y-m-d');

            $maintenance=Maintenance::where('admin_id',$admin_id)

		->where('due_from', '>', $current_date)
		->where('due_to', '<', $current_date)
		->get()->toArray();

It is up to you if you want to use < or <=. Likewise > or >=

deansatch's avatar

without the date queries does it return any results? Break it down. First make sure you are getting results unfiltered, then make sure the results contain a date in BOTH columns that would satisfy the query.

i.e. so if you get results, what is due_from, due_to and is current date actually between those two. If the current date is one of those 2 dates and you expect results then you need to use <= and/or >=

1 like
jeevamugunthan's avatar

@deansatch yes i'm sure i get values when i use this query

$maintenance=maintenance::get()->where('admin_id', '=', $admin_id)->toArray();

and in my db the due_from and due_to column dates are

due_from=2020-03-01 and

due_to=2020-03-31

and the current date is 2020-03-23

deansatch's avatar

Looking again at my answer the < and > should be the other way round logically!



		->where('due_from', '<', $current_date)
		->where('due_to', '>', $current_date)

deansatch's avatar
Level 24

	$current_date=date('Y-m-d'); // '2020-03-23'

            $maintenance=Maintenance::where('admin_id',$admin_id) // we know this returns results
			//so filter...
		->where('due_from', '<', $current_date) // where $current_date is after due_from
		->where('due_to', '>', $current_date) // AND where $current_date is before due_to
		->get()->toArray();

However, if you want to include the date e.g. if today was 2020-03-01 and the due_from was 2020-03-01 and should still show up, do this instead:


$maintenance=Maintenance::where('admin_id',$admin_id) // we know this returns results
			//so filter...
		->where('due_from', '<=', $current_date) // where $current_date is after or the same as due_from
		->where('due_to', '>=', $current_date) // AND where $current_date is before or the same as due_to
		->get()->toArray();

1 like
deansatch's avatar

sorry for taking the long route to get there :/. No matter how much I work with dates they still mess with my mind!

Please or to participate in this conversation.