- var_dump: documentation ( source)
- strtotime: documentation ( source)
<?php
$data = [
[
'ID' => 16,
'begin_date' => new \DateTime('2016-01-01 UTC'),
'end_date' => new \DateTime('2016-11-30 UTC'),
],
[
'ID' => 33,
'begin_date' => new \DateTime('2016-01-04 UTC'),
'end_date' => new \DateTime('2016-02-29 UTC'),
],
];
// for programs that run over multiple seconds this makes sure we don't drift during sorting
$now = new \DateTime(strtotime('now'));
// assume length >= 1 w/ previous validation
// keep track of the in-progress leading element, assuming the first is leading to begin with
$currentLeading = $data[0];
foreach($data as $index => $element) {
if($element['begin_date'] > $now) {
// first condition is true
if ($index == 0) {
echo "Error: Element cannot be the leading element";
// more likely return false if wrapped in a validation function
}
if($element['end_date'] > $now) {
if ($element['end_date'] > $currentLeading['end_date']) {
$currentLeading = $element;
}
}
}
}
var_dump($currentLeading);