how to check current date is between two dates in laravel 5.4
Here you can use laravel whereRaw() to achieve this.
Just like this
$dt = Carbon::now();
$getmonths= DB::table('Financial_Year')
->whereRaw('"'.$dt.'" between `start_date` and `End_date`')
->get();
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.
Laravel 5: Check if event date range is not already taken
You can use following code to get the correct counts for events booked between certain times:
$eventsCount = Events::where(function ($query) use ($startTime, $endTime) {
$query->where(function ($query) use ($startTime, $endTime) {
$query->where('start', '>=', $startTime)
->where('end', '<', $startTime);
})
->orWhere(function ($query) use ($startTime, $endTime) {
$query->where('start', '<', $endTime)
->where('end', '>=', $endTime);
});
})->count();
How to check if 2 dates and time ranges overlaps in laravel eloquent
you need check conditions :
Time request from user :
A : beginning_time, B : end_time
This has 4 case match with records from DB:
Y : beginning_time_record, Z : end_time_record
case 1 :
Y -> A -> Z -> B
case 2 :
Y -> A -> B -> Z
case 3 :
A -> Y -> Z -> B
case 4 :
A -> Y -> B -> Z
same as :
$existReservation = Reservation::orWhere(function ($q1) use ($beginningDatetime, $endDatetime) {
$q1->where('beginning_date_time', '>=', $beginningDatetime )
->where('beginning_date_time', '<=', $endDatetime);
})
->orWhere(function ($q2) use ($beginningDatetime, $endDatetime) {
$q2->where('end_date_time', '>=', $beginningDatetime )
->where('end_date_time', '<=', $endDatetime);
})
->orWhere(function ($q3) use ($beginningDatetime, $endDatetime) {
$q3->where('beginning_date_time', '>=', $beginningDatetime)
->where('end_date_time', '<=', $endDatetime);
})
->orWhere(function ($q4) use ($beginningDatetime, $endDatetime) {
$q4->where('beginning_date_time', '<=', $beginningDatetime)
->where('end_date_time', '>=', $endDatetime);
})
->count();
or group conditions :
$existReservation = Reservation::orWhere(function ($q1) use ($beginningDatetime, $endDatetime) {
$q1->where('beginning_date_time', '>=', $beginningDatetime )
->where('beginning_date_time', '<=', $endDatetime);
})
->orWhere(function ($q2) use ($beginningDatetime, $endDatetime) {
$q2->where('beginning_date_time', '<=', $beginningDatetime )
->where('end_date_time', '>=', $beginningDatetime );
})
->count();
for check no availability
Update : other way :
$existReservation = Reservation::where('end_date_time', '>=', $beginningDatetime)
->where('beginning_date_time', '<=', $endDatetime)
->count();
Database : beginning_date_time and end_date_time
Laravel 7: Find days and dates between two dates with condition
It's actually easier. You don't need to complicate it. Carbon
comes to the rescue here.
$startDate = Carbon::parse('start date here');
$endDate = Carbon::parse('date string here');
$weekdays = $startDate->diffInWeekdays($endDate);
This will give you the day difference in weekdays excluding any weekends.
Now whichever database you use, you would have a function to get the day of the week in number where 0 stands for Sunday
and 6 for Saturday
.
For Postgres, I will give you an example
weekdays_holiday_count = select count(*) from holidays where date_part('dow', date) not in (0, 6) # 0 for Sunday and 6 for Saturday
This query is counting holidays which came on weekdays. I give you the raw query here. however, the point here is to under the magic of the function.
Now you can do the math easily
$weekdays - $weekdays_holiday_count
Make sure to import Carbon\Carbon
at the top.
Related Topics
How to Remove Duplicate Values from a Multi-Dimensional Array in PHP
How to Get the Client Ip Address in PHP
Send Email Using the Gmail Smtp Server from a PHP Page
How to Use Return Inside a Recursive Function in PHP
How to Send an Email Using PHP
Invalid Argument Supplied For Foreach()
Transposing Multidimensional Arrays in PHP
How to Get a Youtube Video Thumbnail from the Youtube API
Warning: MySQL_Fetch_Array(): Supplied Argument Is Not a Valid MySQL Result
How to Catch a PHP Fatal ('E_Error') Error
How to Show Checkboxes as Checked When Values Are Set in the Database in Laravel
How to Change PHP Version in Xampp
Warning: Mysqli_Connect(): (Hy000/1049): Unknown Database in MAC Terminal Only
How to Run a PHP Script in the Background After a Form Is Submitted
Convert Svg Image to Png With PHP