user3818576
user3818576

Reputation: 3391

Get the last timein of array

I have problem in my array. How can I get the lastest time from array? As you can see in my sample code

Array
(
    [0] => 2015-08-04 01:00:00
    [1] => 2015-08-03 16:00:00
    [2] => 2015-08-03 10:00:00
)

That is just a sample result of my list. It can change anytime. It depends on my setting. I tried to solve this, but don't have any idea to change the array. I want to get the

2015-08-04 01:00:00

Hoping for your help. I know only strtotime().

Upvotes: 2

Views: 37

Answers (2)

MH2K9
MH2K9

Reputation: 12039

You can use usort() to sort date by ASC or DESC then get the last time. Example

$array = array(
    '2015-08-04 01:00:00',
    '2015-08-03 16:00:00',
    '2015-08-03 10:00:00',
);

usort($array, function($a, $b) {
    return strtotime($a) - strtotime($b);
});

$lastTime = end($array);

print $lastTime;

Upvotes: 2

FuzzyTree
FuzzyTree

Reputation: 32392

Because your timestamps are formatted as yyyy-mm-dd hh:ii:ss you can use max to get latest time in the array

$latest = max($values);
print $latest;

Upvotes: 1

Related Questions