Skip to content

Instantly share code, notes, and snippets.

@schmiddim
Last active August 29, 2015 14:20
Show Gist options
  • Save schmiddim/09ffd20f1a247648c0ed to your computer and use it in GitHub Desktop.
Save schmiddim/09ffd20f1a247648c0ed to your computer and use it in GitHub Desktop.
<?php
public function testGetMax52Weeks() {
$d = new DateTime('2015-08-01 00:00:00');
$date52WeeksAgo = $d->modify('-52 weeks');
var_dump($date52WeeksAgo->format(DateTime::ISO8601));
$dates =[];
$dates[] = '2014-08-01 00:00:00';
$dates[] = '2014-08-04 00:00:00';
$dates[] = '2014-08-05 00:00:00';
$dates[] = '2014-08-06 00:00:00';
$dates[] = '2014-08-07 00:00:00';
$dates[] = '2014-08-08 00:00:00';
$indexClosest =null;
$lowestInterval=-1;
foreach($dates as $index => $date){
if($indexClosest === null ) {
$indexClosest = $index;
}
$interval =$date52WeeksAgo->diff(new DateTime($date))->format('%R%a')*1;
if($interval >0 &&($lowestInterval <0 || $interval<$lowestInterval )){
$lowestInterval = $interval;
$indexClosest = $index;
}
}
var_dump($dates[$indexClosest]);
/**
* output
* string(24) "2014-08-02T00:00:00+0200"
* string(19) "2014-08-04 00:00:00"
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment