Reputation:
I know I can get a year from a data using carbon like so.
This gets the current year.
$current = Carbon::now();
$year = Carbon::createFromFormat('Y-m-d H:i:s', $current)->year;
I have a query which gets all the appointments a user has had. I need to get all appointments had in the current calendar year.
I have the following query
$appointments = Appointments::where('id', $id)
->where('event_end_time', '<', $current)
->count();
How can i check get the event end times (which is a date) year. For example If the date is 11-05-2018 and can I just get the year (2018) and check if a appointment happened in 2018
Upvotes: 1
Views: 408
Reputation: 103
You can also use SQL DATE_FORMAT function.
$appointments = Appointments::where('id', $id)
->where(DB::raw('DATE_FORMAT(event_end_time, "%Y-%m-%d")), '<', $current)
->count();
I think this is better than calculating date by php, as it is done by Database.
Upvotes: 0
Reputation: 35377
Laravel's query builder includes a method for this:
whereYear('event_end_time', $year)
In raw SQL this is where year(event_end_time) = ?
Upvotes: 1