How to check if a date is in the future, for separate date and time columns?
P粉021854777
P粉021854777 2023-07-31 19:18:47
0
1
549
<p>I have two columns for storing dates: one is the date itself in YYYY-mm-dd format, and the other is the time in time(7) data type, e.g. 11:15:10.0000000. <br /><br />How do I check for future rows? <br /><br />I can get the first part, the date itself: </p><p><br /></p> <pre class="brush:php;toolbar:false;">MyModel::where('date', '>=', Carbon::today())->get()</pre> <p>But when I try to add the time, it doesn't work: </p> <pre class="brush:php;toolbar:false;">MyModel::where('date', '>=', Carbon::today())->where('time', '> ;', Carbon::now()->format('H:i'))->get()</pre> <p>Because they are separate, even if the date is in the future, the time is separate, so there may be a time mismatch. So I need to somehow merge the date and the time associated with it together, not separately. </p>
P粉021854777
P粉021854777

reply all(1)
P粉651109397

Try to combine two columns into one condition.

$now = Carbon::now();

MyModel::whereRaw("CONCAT(`date`, ' ', `time`) >= ?", [$now->toDateTimeString()])->get();

For SQL Server, try the following.

MyModel::whereRaw("CONVERT(datetime, date + ' ' + CONVERT(varchar, time, 121)) >= ?", [$now->toDateTimeString()])->get();

Query transformation may need updating, please consult the documentation for more information.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template