Get Most Recent Date from an Array of Dates in "Y-M-D H:I:S" Format

Get most recent date from an array of dates in Y-m-d H:i:s format

Do a loop, convert the values to date, and store the most recent, in a var.

$mostRecent= 0;
foreach($dates as $date){
$curDate = strtotime($date);
if ($curDate > $mostRecent) {
$mostRecent = $curDate;
}
}

something like that... you get the idea
If you want most recent BEFORE today :

$mostRecent= 0;
$now = time();
foreach($dates as $date){
$curDate = strtotime($date);
if ($curDate > $mostRecent && $curDate < $now) {
$mostRecent = $curDate;
}
}

Ordering an Array of Dates from most recent to oldest whilst changing the other arrays to match the order

Looking at your code, the mistake is having the additional arrays (dates, sleeps, and timeUploadeds). The days array is everything you need. If you want to order it, just sort days:

let sortedDays = days.sorted(by: { $0.date < $1.date} )

Then you can do whatever you need with sortedDays. If for some specific problem you need a list of just the dates or just the sleeps, you can get that any time you need it with map:

let dates = sortedDays.map { $0.date }

Get all dates from last 30 days (datetime objects)

Change

$begin = $today->sub(new DateInterval('P30D'));

to

$begin = new DateTime();
$begin->sub(new DateInterval('P30D'));

find the most recent value between some date in php

Something like this would probably work for you. Look into the function strtotime.

$most_recent_time = 0;
$most_recent_id = NULL;
foreach($dates_arr as $date_obj){
$parsed_time = strtotime($date_obj->createdDateUTC);
if($parsed_time > $most_recent_time && ($parsed_time >= (time() - 3600)){
$most_recent_time = $parsed_time
$most_recent_id = $date_obj->id;
}
}

Haven't tested this code but $most_recent_id should contain the id of the most recent timestamp that is no more than 1 hour ago or NULL if none exist.

How can I find the maximum and minimum date in an array?

<?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];

?>

PHP: Return all dates between two dates in an array

You could also take a look at the DatePeriod class:

$period = new DatePeriod(
new DateTime('2010-10-01'),
new DateInterval('P1D'),
new DateTime('2010-10-05')
);

Which should get you an array with DateTime objects.

To iterate

foreach ($period as $key => $value) {
//$value->format('Y-m-d')
}


Related Topics



Leave a reply



Submit