0

I have a table which consist of ddate and time_start column and I wanted to compare it to today.

In SQL Server, I can directly cast ddate and timestart to format my desired date and compare it to GETDATE().

In my case, how do I compare the separate (ddate and time_start) to today?

EDITED
What I wanted is to concatenate it like '2020-06-01 11:23:00' and then compare it to today

6
  • 1
    Do you want to compare them in PHP or with query builder? Commented Jun 1, 2021 at 15:55
  • 1
    @shaedrich query builder. sorry forgot to mention this. Commented Jun 1, 2021 at 15:56
  • You mean ddate and time_start's type are all TIMESTAMP? Commented Jun 1, 2021 at 15:56
  • In that case you can do it exactly like you'd do in normal SQL. Depending on your logic, you might want to use a whereRaw() Commented Jun 1, 2021 at 15:59
  • @shaedrich Can you show an example? Commented Jun 1, 2021 at 16:00

1 Answer 1

1

The content of whereRaw() should be similar to what you're doing in plain SQL. If not, feel free to customize it.

DB::table('your_table')
    ->whereRaw('CONCAT(ddate, " ", time_start) = GETDATE()')
Sign up to request clarification or add additional context in comments.

3 Comments

Exactly what I'm concerned about if I can directly concat the column. Thanks, man.
one last thing tho :), how can I convert time_start which is AM/PM format to 24 hour format. Normally one would use date('H:i:s', strtotime(time_start), but it gives me error.
That's because you're in SQL not PHP. It seems that MySQL can't really parse 12-hour format. Can't you store the time in 24-hour format and only output it in 12-hour format? I bet, that'd make it much easier.

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.