Reputation:
I have this code to check for consecutive dates in PHP:
$dates = "2017-02-26, 2017-02-27, 2017-03-01";
$dates = explode(',', $dates);
$conseq = array();
$ii = 0;
$max = count($dates);
for($i = 0; $i < count($dates); $i++) {
$conseq[$ii][] = date('Y-m-d',$dates[$i]);
if($i + 1 < $max) {
$dif = $dates[$i + 1] - $dates[$i];
if($dif >= 90000) {
$ii++;
}
}
}
print_r($dates);
The problem is this code do not make the work at 100%.
It should make one array with the consecutive days and another one for each separate days like this:
Array (
[0] => 2017-02-26
[1] => 2017-02-27
)
Array (
[0] => 2017-03-01
)
Anyone to help me please.
Thanks.
Upvotes: 0
Views: 1513
Reputation: 1267
You can put it in foreach loop something like this and then filter the array values by month.
$dates = "2017-02-26, 2017-02-27, 2017-03-01";
$date_arr = explode(',', $dates);
$filter = array();
foreach( $date_arr as $date ){
$filter[date('m', strtotime($date))][] = $date;
}
echo '<pre>'; print_r($filter);
Upvotes: 0
Reputation: 1174
Please try below answer
<?php
$dates = "2017-02-26, 2017-02-27, 2017-03-01";
$dates = explode(',', $dates);
$conseq = array();
$ii = 0;
$max = count($dates);
for($i = 0; $i < count($dates); $i++) {
$conseq[$ii][] = date('Y-m-d',strtotime($dates[$i]));
if($i + 1 < $max) {
$dif = strtotime($dates[$i + 1]) - strtotime($dates[$i]);
if($dif >= 90000) {
$ii++;
}
}
}
print_r($conseq);
?>
Upvotes: 1