0

I have a date in the following format : ('Y-m-d') I'm trying to do a query that brings all rows in a certain date but only from 12:00 AM to 1:00 AM

The following will bring the required data but for today. I need the same but for the entered date $sel_date. How can I replace the carbon::now() with $sel_date ?

->whereBetween('created_at', [
             \Carbon\Carbon::now()->format('Y-m-d 00:00:00'),
             \Carbon\Carbon::now()->addHours(1)->format('Y-m-d 01:00:00')
                    ])     
2
  • 1
    Edit the question and add the $sel_date variable Commented Oct 17, 2019 at 17:19
  • If you're specifying the hour, you don't need to do addHours(1) Commented Oct 17, 2019 at 17:33

2 Answers 2

2
->whereBetween('created_at', [
             \Carbon\Carbon::parse($sel_date)->format('Y-m-d 00:00:00'),
             \Carbon\Carbon::parse($sel_date)->addHours(1)->format('Y-m-d 01:00:00')
                    ])
Sign up to request clarification or add additional context in comments.

Comments

1

Just add 12 hours for 12 am and 1 hour for 1 am to a created instance of carbon from your date variable

Given this user object for example

=> App\User {#3065
     id: 1,
     name: "something",
     email: "[email protected]",
     email_verified_at: null,
     created_at: "2019-10-17 13:16:25",
     updated_at: "2019-10-17 13:16:25",
   }

This query returns it

$sel_date = '2019-10-17';
return User::whereBetween('created_at', [
    Carbon::createFromDate($sel_date)->addHours(13),
    Carbon::createFromDate($sel_date)->addHours(14),
])->get();

Hope this helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.