Skip to content

Instantly share code, notes, and snippets.

@tamakiii
Created October 20, 2015 17:01
Show Gist options
  • Save tamakiii/a665eadaec4b051a9ea4 to your computer and use it in GitHub Desktop.
Save tamakiii/a665eadaec4b051a9ea4 to your computer and use it in GitHub Desktop.
<?php
/**
* @param array $reports
* @param string $start
* @param string $end
* @return array
*/
function timeFilter(array $reports, $start, $end)
{
$start = strtotime($start);
$end = strtotime($end);
return array_filter($reports, function(array $report) use ($start, $end) {
$current = strtotime($report['date']) ;
return $start <= $current && $current < $end;
});
}
$reports = [
['id' => 1, 'date' => '2015-01-01 00:00:00'],
['id' => 2, 'date' => '2015-01-02 00:00:00'],
['id' => 3, 'date' => '2015-01-03 00:00:00'],
['id' => 4, 'date' => '2015-01-04 00:00:00'],
['id' => 5, 'date' => '2015-01-05 00:00:00'],
];
var_export(timeFilter($reports, '2015-01-02', '2015-01-05'));
# => array (
# 1 =>
# array (
# 'id' => 2,
# 'date' => '2015-01-02 00:00:00',
# ),
# 2 =>
# array (
# 'id' => 3,
# 'date' => '2015-01-03 00:00:00',
# ),
# 3 =>
# array (
# 'id' => 4,
# 'date' => '2015-01-04 00:00:00',
# ),
# )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment