Laravel - where less/greater than date syntax
Use a Carbon instance:
$this->data['Tasks'] = \DB::table('tb_tasks')->where('Status', 'like', 'Open%')->whereDate('DeadLine', '>', Carbon::now())->count();
You can also use the now()
helper
$this->data['Tasks'] = \DB::table('tb_tasks')->where('Status', 'like', 'Open%')->whereDate('DeadLine', '>', now())->count();
Laravel get record with date greater than today
You missed the query condition. Do this:
$date = today()->format('Y-m-d')
Action::where('to_date', '>=', $date)->get()
Laravel Eloquent find rows where date older than 2 days
You can use Carbon subDays()
like below:
$data = YOURMODEL::where('created_at', '<=', Carbon::now()->subDays(2)->toDateTimeString())->get();
Laravel query check date lesser than end date only if Not Null
How about adding a whereRaw with an orWhere statement? Also, I noticed that you said you wanted to get values that are greater than start_at
and lesser than end_at
, but you had start_at <= $effective_date
and end_at >= $effective_date
. I might have misunderstood you wrong though, but I changed the relations to match what your description said.
$effectiveDate = '2022-04-06 23:34:00'
GroupExtras::query()
->select('group_extras.*')
->join('groups', 'group_extras.group_id', 'groups.id')
->where(function ($query) use ($effectiveDate) {
$query->whereRaw('group_extras.start_at > ' . $effectiveDate . ' AND (group_extras.end_at is not null and group_extras.end_at < ' . $effectiveDate . ')');
$query->orWhere('group_extras.start_at', '>', $effectiveDate);
})
->orderBy('group_extras.value')
->first();
Laravel query where date more recent than current time
it looks like you need a query scope (https://laravel.com/docs/5.6/eloquent#local-scopes).
assuming 'start_time' is a model property (database field) containing a some representation of time, and you want a scope that returns all models where 'start_time' later than now
...
How you structure your code depends on what format your dates are stored in your database.
For example, if you are using epoch timestamps, then in your Model.php:
public function scopewhereAfterNow($query)
{
return $query->where('start_time', '>', \Carbon\Carbon::now()->timestamp);
}
or you could use the DB facade:
public function scopewhereAfterNow($query)
{
return $query->where('start_time', '>', DB::raw('unix_timestamp(NOW())'));
}
which you might call like:$results = Model::whereAfterNow()->get();
Laravel Eloquent compare date from datetime field
Laravel 4+ offers you these methods: whereDay()
, whereMonth()
, whereYear()
(#3946) and whereDate()
(#6879).
They do the SQL DATE()
work for you, and manage the differences of SQLite.
Your result can be achieved as so:
->whereDate('date', '<=', '2014-07-10')
For more examples, see first message of #3946 and this Laravel Daily article.
Update: Though the above method is convenient, as noted by Arth it is inefficient on large datasets, because the DATE()
SQL function has to be applied on each record, thus discarding the possible index.
Here are some ways to make the comparison (but please read notes below):
->where('date', '<=', '2014-07-10 23:59:59')
->where('date', '<', '2014-07-11')
// '2014-07-11'
$dayAfter = (new DateTime('2014-07-10'))->modify('+1 day')->format('Y-m-d');
->where('date', '<', $dayAfter)
Notes:
- 23:59:59 is okay (for now) because of the 1-second precision, but have a look at this article: 23:59:59 is not the end of the day. No, really!
- Keep in mind the "zero date" case ("0000-00-00 00:00:00"). Though, these "zero dates" should be avoided, they are source of so many problems. Better make the field nullable if needed.
How to query between two dates using Laravel and Eloquent?
The whereBetween
method verifies that a column's value is between
two values.
$from = date('2018-01-01');
$to = date('2018-05-02');
Reservation::whereBetween('reservation_from', [$from, $to])->get();
In some cases you need to add date range dynamically. Based on @Anovative's comment you can do this:
Reservation::all()->filter(function($item) {
if (Carbon::now()->between($item->from, $item->to)) {
return $item;
}
});
If you would like to add more condition then you can use orWhereBetween
. If you would like to exclude a date interval then you can use whereNotBetween
.
Reservation::whereBetween('reservation_from', [$from1, $to1])
->orWhereBetween('reservation_to', [$from2, $to2])
->whereNotBetween('reservation_to', [$from3, $to3])
->get();
Other useful where clauses: whereIn
, whereNotIn
, whereNull
, whereNotNull
, whereDate
, whereMonth
, whereDay
, whereYear
, whereTime
, whereColumn
, whereExists
, whereRaw
.
Laravel docs about Where Clauses.
WHERE clause date is greater than 24h - Carbon / Laravel
It looks like you might be using the wrong operator. <
(Less than) should be swapped for >
(Greater than):
$reminder = Reminder::where('reminder_date','>', Carbon::now()->addHours(24))
->get();
Related Topics
Deleting an Element from an Array in PHP
PHP Pass Variable to Next Page
How Does PHP 'Foreach' Actually Work
Invalid Argument Supplied For Foreach()
Warning: MySQL_Fetch_Array(): Supplied Argument Is Not a Valid MySQL Result
Why Would One Omit the Close Tag
How to Access and Manipulate Multi-dimensional Array by Key Names/Path
How to Squeeze Error Message Out of Pdo
Are Global Variables in PHP Considered Bad Practice? If So, Why
What Are Long-polling, Websockets, Server-sent Events (Sse) and Comet
MySQL Great Circle Distance (Haversine Formula)
PHP - Copy Image to My Server Direct from Url
How to Send a Post Request With PHP
Php Substring Remove Everything in a String After Certain Character