I
I
Issue2021-06-15 22:11:03
PHP
Issue, 2021-06-15 22:11:03

How to determine the first and last date in an array in php?

Tried to compare:

$start = '0000-00-00';
$end = '0000-00-00';

if ($date > $start) {
    $start = $date;
}
if ($date < $end) {
    $end = $date;
}


But for some reason it does not work ...
I tried in different ways, but I could not figure out how to display the earliest date from the array and the latest.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2021-06-15
@Rsa97

$start = '9999-99-99';
$end = '0000-00-00';

foreach ($dates as $date) {
    if ($date < $start) {
        $start = $date;
    }
    if ($date > $end) {
        $end = $date;
    }
}

M
Maxim, 2021-06-15
@Tomio

Use the DateTime class:

$start = new DateTime('2021-04-14');
$end = new DateTime('2021-06-14');
$date = new DateTime('today');
if ($date > $start) {
    $start = $date;
}
if ($date < $end) {
    $end = $date;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question