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

kaiju's avatar
Level 1

Static date range filter

Hello guys, i don't know what approach I'm going to do here. I want to filter data using date today, but if I continually refresh my url the date today is also updating to current time, and the problem is it's not getting the data that just added recently in the database.

I'm not able to get the filter i desired for date today, because the date value of inserted in the database is not current.

Controller:

    $myTime = Carbon\Carbon::now()->toDateTimeString();

    if(request()->has('date'))
    {
        $statView = request()->date;
        
        $data = DB::table('servicerequest as sr')
        ->leftjoin('transact as t', 'sr.id' , '=' , 't.request_id')
        ->join('codes as c', 'sr.id' , '=' , 'c.request_id')
        ->orderBy('status','asc')
        ->orderBy('created_at','asc')
        ->select('c.request_code','sr.*' , 't.status','sr.created_at as ca','t.user_id as ui')
        ->where('t.created_at',$statView)
        ->paginate(10)
        ->appends('date',request('date'));
    }

    return view('page.monitoring')
    ->with('date',$myTime); 

View:

<a href="/monitoring/?date={{$date}}" class="btn btn-5 " data-value="">Request Today </a>

URL: picture is too small to read, here's the url format: localhost:8000/monitoring/?date=2017-09-11%2023:19:00

Database value:

0 likes
4 replies
mattsplat's avatar

If I understand you it seems you don't want the time stamp.

 $myTime = Carbon\Carbon::today()->toDateTimeString();

    if(request()->has('date'))
    {
        $statView = request()->date;
        
        $data = DB::table('servicerequest as sr')
        ->leftjoin('transact as t', 'sr.id' , '=' , 't.request_id')
        ->join('codes as c', 'sr.id' , '=' , 'c.request_id')
        ->orderBy('status','asc')
        ->orderBy('created_at','asc')
        ->select('c.request_code','sr.*' , 't.status','sr.created_at as ca','t.user_id as ui')
        ->whereDate('t.created_at',$statView)
        ->paginate(10)
        ->appends('date',request('date'));
    }

    return view('page.monitoring')
    ->with('date',$myTime); 

kaiju's avatar
Level 1

yes if possible i only want the date like this "2017-09-11" but it will sort it base on who inserted first, so how will i do it without using the time?

mattsplat's avatar
Level 8

->orderBy('created_at','asc') should still sort by the timestamp. Using whereDate should compare only the date from created_at. So you should have all the matches for the date ordered by timestamp.

If you only want to return the date without the trailing time you can use.

date('Y-m-d')

Please or to participate in this conversation.