Skip to content

Instantly share code, notes, and snippets.

@xorenio
Created March 14, 2017 01:20
Show Gist options
  • Save xorenio/3c8cce4ba99300a33a523bb2d9f10b6b to your computer and use it in GitHub Desktop.
Save xorenio/3c8cce4ba99300a33a523bb2d9f10b6b to your computer and use it in GitHub Desktop.
Laravel birthday list from date of birth
<?php
$thisDayNo = Carbon::now()->day;
$thisMonthNo = Carbon::today()->month;
$nextMonthNo = $thisMonthNo+1;
if($nextMonthNo > 12)
$nextMonthNo = 1;
$ClientBirthday = Client::whereNull('serviceEndDate') // make sure their still in contract
->whereBetween(DB::raw('MONTH(client_dob)'), [Carbon::today()->month,Carbon::today()->month+1])
// selecting and changing dates to remove the year also with a order number for ordering at the end
->select('client_dob', DB::raw('concat (client_last_name," ",client_first_name) as name'), DB::raw('DATE_FORMAT(client_dob, "%D %b") as day'), DB::raw('DATE_FORMAT(client_dob, "%m-%d") as orderNo'))
// compare dates where {where or where} might be easier way to do this
->where(function ($query) use ($thisMonthNo,$nextMonthNo,$thisDayNo) {
$query->where(function ($q1) use ($thisMonthNo,$thisDayNo) {
$q1->where(DB::raw('MONTH(client_dob)'), $thisMonthNo)
->where(DB::raw('DAY(client_dob)'), '>=', $thisDayNo);
})
->orWhere(function ($q2) use ($nextMonthNo,$thisDayNo) {
$q2->where(DB::raw('MONTH(client_dob)'), $nextMonthNo)
->where(DB::raw('DAY(client_dob)'), '<=', $thisDayNo);
});
})
->orderby('orderNo', 'ASC')->get();
// was made in laravel 4 back in the days ^_^
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment