Reputation: 59
Following is my code to bring all the latest dates in a month from dates array
$dates = Array
(
"2017/03/05",
"2017/03/06",
"2017/04/01",
"2017/04/16",
"2017/06/16",
"2017/06/19",
"2017/07/07",
"2017/08/19",
);
$curr_val = '';
$years = Array();
$months = Array();
foreach($dates as $d) {
list($y,$m) = explode("/",$d);
$years[$y][] = $d;
if ($m != $curr_val){
$months[$y."/".$m][] = $d;
$curr_val = $m ;
}
}
$years = array_values($years);
$months = array_values($months);
print_r($months);
Above code is printing values of first occurance date in a month. But i want the latest date from month.
Above code gives following output
Array ( [0] => Array ( [0] => 2017/03/05 ) [1] => Array ( [0] => 2017/04/01 ) [2] => Array ( [0] => 2017/06/16 ) [3] => Array ( [0] => 2017/07/07 ) [4] => Array ( [0] => 2017/08/19 ) )
But expected code is
Array ( [0] => Array ( [0] => 2017/03/06 ) [1] => Array ( [0] => 2017/04/16 ) [2] => Array ( [0] => 2017/06/19 ) [3] => Array ( [0] => 2017/07/07 ) [4] => Array ( [0] => 2017/08/19 ) )
Instead of printing first date in month, i want last date of month to be printed. how can i get that??
Upvotes: 0
Views: 91
Reputation: 47903
This seems simplest to me:
Code: (Demo)
$dates[
"2017/03/05",
"2017/03/06",
"2017/04/01",
"2017/04/16",
"2017/06/16",
"2017/06/19",
"2017/07/07",
"2017/08/19"
];
foreach($dates as $d){
$result[substr($d,0,7)]=$d; // extract unique key from values
}
$result=array_values($result); // reindex array
var_export($result);
Output:
array (
0 => '2017/03/06',
1 => '2017/04/16',
2 => '2017/06/19',
3 => '2017/07/07',
4 => '2017/08/19',
)
Upvotes: 0
Reputation: 41810
Like this:
foreach($dates as $d) {
list($y,$m) = explode("/",$d);
$years[$y][] = $d;
$months[$y."/".$m] = [$d];
}
Don't worry about keeping track of the current value. Just keep overwriting $months[$y."/".$m]
as you go, and you'll end up with the last date for each month.
If you want to get the most recent date from each month, rather than just the last one in the input array, make sure $dates
is sorted before you start the loop. Since they're in year/month/day format, a simple sort($dates);
should work. (In the case of your example, $dates
is already sorted so this won't make any difference.)
Upvotes: 1
Reputation: 1165
Your code could be reduced a lot. Here's what I could do:
$dates = Array
(
"2017/03/05",
"2017/03/06",
"2017/04/01",
"2017/04/16",
"2017/06/16",
"2017/06/19",
"2017/07/07",
"2017/08/19",
);
$array = [];
foreach($dates as $d) {
$date = \DateTime::createFromFormat('Y/m/d' , $d);
if (
!isset($array[$date->format('Y')][$date->format('m')])
|| $array[$date->format('Y')][$date->format('m')] < $date
) {
$array[$date->format('Y')][$date->format('m')] = $date;
}
}
print_r($array);
It's a simple logic that will check if the value is higher inside the same year/month and replace if it is.
Result:
Array ( [2017] => Array ( [03] => DateTime Object ( [date] => 2017-03-06 22:18:42.000000 [timezone_type] => 3 [timezone] => Europe/Amsterdam ) [04] => DateTime Object ( [date] => 2017-04-16 22:18:42.000000 [timezone_type] => 3 [timezone] => Europe/Amsterdam ) [06] => DateTime Object ( [date] => 2017-06-19 22:18:42.000000 [timezone_type] => 3 [timezone] => Europe/Amsterdam ) [07] => DateTime Object ( [date] => 2017-07-07 22:18:42.000000 [timezone_type] => 3 [timezone] => Europe/Amsterdam ) [08] => DateTime Object ( [date] => 2017-08-19 22:18:42.000000 [timezone_type] => 3 [timezone] => Europe/Amsterdam ) ) )
Upvotes: 1