I need to find the maximum and minimum date from a given array using PHP.
I have $date_arr which contains following values,
$date_arr = array('0'=>'20-05-2015','1'=>'02-01-2015','2'=>'30-03-2015');
Here, I need to get the larger date as '20-05-2015' and the minimum date as '02-01-2015'.
How can I achieve this?
<?php
$date_arr=array(0=>'20-05-2015',1=>'02-01-2015',2=>'30-03-2015');
usort($date_arr, function($a, $b) {
$dateTimestamp1 = strtotime($a);
$dateTimestamp2 = strtotime($b);
return $dateTimestamp1 < $dateTimestamp2 ? -1: 1;
});
echo 'Min: ' . $date_arr[0];
echo '<br/>';
echo 'Max: ' . $date_arr[count($date_arr) - 1];
?>
max()
and min()
works fine with your array:
echo "Latest Date: ". max($dates)."\n";
echo "Earliest Date: ". min($dates)."\n";
please Try this
$date_arr = array('0' => '20-05-2015', '1' => '02-01-2015', '2' => '30-03-2015');
for ($i = 0; $i < count($date_arr); $i++)
{
if ($i == 0)
{
$max_date = date('Y-m-d H:i:s', strtotime($date_arr[$i]));
$min_date = date('Y-m-d H:i:s', strtotime($date_arr[$i]));
}
else if ($i != 0)
{
$new_date = date('Y-m-d H:i:s', strtotime($date_arr[$i]));
if ($new_date > $max_date)
{
$max_date = $new_date;
}
else if ($new_date < $min_date)
{
$min_date = $new_date;
}
}
}
echo date('d-m-Y',strtotime($max_date));
echo date('d-m-Y',strtotime($min_date));
Thought it doesn't technically offer the lowest computational time complexity, array_multisort()
is a sensible, readable, concise approach. My snippet only calls strtotime()
on each element once -- usort()
cannot match this claim.
Code: ( Demo )
$dates = ['20-05-2015', '02-01-2015', '30-03-2015', '10-01-1990'];
array_multisort(array_map('strtotime', $dates), $dates);
printf(
"Latest Date: %s\nEarliest Date: %s",
$dates[array_key_last($dates)],
$dates[0]
);
Output:
Latest Date: 10-01-1990
Earliest Date: 20-05-2015
To arrive at the same result by calling min()
and max()
, just create a formatted copy of the dates in unix time.
Code: ( Demo )
$unix = array_map('strtotime', $dates);
printf(
"Latest Date: %s\nEarliest Date: %s",
date('d-m-Y', max($unix)),
date('d-m-Y', min($unix))
);
If you want to use usort()
, here is the most modern syntax with the spaceship operator and arrow function syntax. ( Demo )
usort($dates, fn($a, $b) => strtotime($a) <=> strtotime($b));
printf(
"Latest Date: %s\nEarliest Date: %s",
$dates[array_key_last($dates)],
$dates[0]
);
This task can surely be accomplished tens of different ways. I considered writing a foreach()
loop with conditions to maintain temporary variables while making iterated comparisons, but I felt it was prohibitively convoluted for a rather simple task.
$date_arr=array(0=>'2015-05-20',1=>'2015-02-21',2=>'2015-04-13',3=>'2020-04-30',4=>'2020-04-13');
$max_date=$date_arr[0];
for($i=0;$i<count($date_arr);$i++)
{
echo $date_arr[$i]. ' ,';
if( $max_date < $date_arr[$i+1])
{
$max_date=$date_arr[$i+1];
}
}
echo " Max= ". $max_date;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.