note 85942 deleted from function.strtotime by derick
| From: | derick@php.net | Date: | Sun, 14 Dec 2014 11:33:42 +0000 |
| Subject: | note 85942 deleted from function.strtotime by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-201165@lists.php.net to get a copy of this message | ||
Note Submitter: orel16 at ya dot ru
----
Some simple function to find all periods in dates list
<?php
function extract_periods($dates,$delimiter="="){
sort($dates);
$period=Array();
$date_from=$date_to="";
$k=0;
foreach ($dates as $key=>$value) {
if (empty($date_from)) $date_from=$value;
else {
$k++;
$tmp_date=strtotime("+ ".$k." day",strtotime($date_from));
if (date("Y-m-d", $tmp_date)!=$value) {
$tmp_date=strtotime("+ ".($k-1)." day",strtotime($date_from))
$date_to=date("Y-m-d",$tmp_date);
if ($date_from!=$date_to)
$period[]=$date_from.$delimiter.$date_to;
else
$period[]=$date_from;
$date_from=$value;
$k=0;
}
}
}
return $period;
}
Use:
$dates = Array(
"2008-09-30",
"2008-09-28",
"2008-09-26",
"2008-09-27",
"2008-09-25");
$periods = extract_periods($dates);
foreach ($periods as $value)
echo $value."<br />";
?>
Output:
2008-09-25=2008-09-28
2008-09-30