Skip to content

Instantly share code, notes, and snippets.

@tamakiii
Created October 20, 2015 17:03
Show Gist options
  • Save tamakiii/eac4c567734f6390615a to your computer and use it in GitHub Desktop.
Save tamakiii/eac4c567734f6390615a 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);
foreach ($reports as $report) {
$current = strtotime($report['date']) ;
if ($start <= $current && $current < $end) {
yield $report;
}
}
}
$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(iterator_to_array(timeFilter($reports, '2015-01-02', '2015-01-05')));
# => array (
# 0 =>
# array (
# 'id' => 2,
# 'date' => '2015-01-02 00:00:00',
# ),
# 1 =>
# array (
# 'id' => 3,
# 'date' => '2015-01-03 00:00:00',
# ),
# 2 =>
# 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