Skip to content

Instantly share code, notes, and snippets.

@Tech-Emiretus
Created May 23, 2018 21:57
Show Gist options
  • Save Tech-Emiretus/c961ac059fe87254dffcc44fe5fed566 to your computer and use it in GitHub Desktop.
Save Tech-Emiretus/c961ac059fe87254dffcc44fe5fed566 to your computer and use it in GitHub Desktop.
Filter data to display only entries made in July
<?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