Created
May 23, 2018 21:57
-
-
Save Tech-Emiretus/c961ac059fe87254dffcc44fe5fed566 to your computer and use it in GitHub Desktop.
Filter data to display only entries made in July
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$data = [ | |
(object) [ | |
'name' => 'Ring [A0123]', | |
'price' => 25, | |
'created_at' => '2016-07-20' | |
], | |
(object) [ | |
'name' => 'Pendant [A0124]', | |
'price' => 55, | |
'created_at' => '2016-07-17' | |
], | |
(object) [ | |
'name' => 'Earrings [A0125]', | |
'price' => 15, | |
'created_at' => '2016-06-20' | |
], | |
(object) [ | |
'name' => 'Bracelet [ABCDE]', | |
'price' => 15, | |
'created_at' => '2016-06-23' | |
] | |
]; | |
/** | |
* Get data from the specified month. | |
* | |
* @param array $data | |
* @param int $month | |
* @return array|bool | |
*/ | |
function getMonthData($data, $month) { | |
if ($month < 1 || $month > 12) { | |
return false; | |
} | |
return array_filter($data, function ($item) use ($month) { | |
$created_date = date_parse($item->created_at); | |
return $created_date['month'] === $month; | |
}); | |
} | |
// Get all data for July = 7 | |
print_r(getMonthData($data, 7)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment