I have an array of results fetched from mysql database. I want to group those data based on dates as below:
Input array:
$resultSet = [
0 => [
'id' => 123,
'name' => 'ABC',
'start_date' => '2019-12-18 20:00:00'
],
1 => [
'id' => 124,
'name' => 'CDE',
'start_date' => '2019-12-19 20:00:00'
],
2 => [
'id' => 125,
'name' => 'TEST',
'start_date' => '2019-12-23 20:00:00'
],
3 => [
'id' => 126,
'name' => 'BWM',
'start_date' => '2019-12-18 20:00:00'
],
4 => [
'id' => 127,
'name' => 'XYZ',
'start_date' => '2019-12-19 20:00:00'
],
5 => [
'id' => 128,
'name' => 'GHJ',
'start_date' => '2019-12-21 20:00:00'
],
6 => [
'id' => 129,
'name' => 'GHJK',
'start_date' => '2019-12-22 20:00:00'
],
7 => [
'id' => 130,
'name' => 'GHL',
'start_date' => '2019-12-20 20:00:00'
],
8 => [
'id' => 131,
'name' => 'JKL',
'start_date' => '2019-12-25 20:00:00'
]
];
Output: Display all the list of products group by filters (Today, Tomorrow, This Weekend, Next Weekend etc.) Consider we are running script on Wednesday.
Today: Display only record of index: 0, 3 (Today's Date: 2019-12-18)
Tomorrow: Display only record of index: 0, 3 (Tomorrow Date: 2019-12-19)
This Weekend: Display only record of index: 5, 6, 7 (Friday, Saturday & Sunday: 2019-12-20, 2019-12-21, 2019-12-22)
Next Week: Display only record of index: 2, 8 (Date: 2019-12-23, 2019-12-25)
Result will display dynamically and every week different record will display as above.
Sample Code Used:
$arrayList = [];
foreach($resultSet as $element){
$arrayList[$element->start_date][] = $element;
}
dd($arrayList);
Need to alter the above code to fetch the result as per the requirement below.
Framework: Laravel 5.2
Requirements:
- Script will either run on Wednesday or Thrusday (90% of time).
- In case for some reason script did not run on those days. We will manually run it on Friday. In that case Today is Friday and Saturday is Tomorrow. Also This Weekend will include data of Friday, Saturday and Sunday.
- This Weekend will include data of Friday, Saturday & Sunday.
- Today, Tomorrow, This Weekend & Next Weekend label will be filtered from the "start_date".
Expected OUTPUT:
$output = [
'today' => [
0 => [
'id' => 123,
'name' => 'ABC',
'start_date' => '2019-12-18 20:00:00'
],
1 => [
'id' => 126,
'name' => 'BWM',
'start_date' => '2019-12-18 20:00:00'
]
],
'tomorrow' => [
0 => [
'id' => 124,
'name' => 'CDE',
'start_date' => '2019-12-19 20:00:00'
],
1 => [
'id' => 127,
'name' => 'XYZ',
'start_date' => '2019-12-19 20:00:00'
]
],
'this_weekend' => [
0 => [
'id' => 130,
'name' => 'GHL',
'start_date' => '2019-12-20 20:00:00'
],
1 => [
'id' => 128,
'name' => 'GHJ',
'start_date' => '2019-12-21 20:00:00'
],
2 => [
'id' => 129,
'name' => 'GHJK',
'start_date' => '2019-12-22 20:00:00'
]
],
'next_week' => [
0 => [
'id' => 125,
'name' => 'TEST',
'start_date' => '2019-12-23 20:00:00'
],
1 => [
'id' => 131,
'name' => 'JKL',
'start_date' => '2019-12-25 20:00:00'
]
]
];
I am running simple foreach loop in view page to display data. Please feel free to comment below if any queries.