1

Working on a small formula to find the closet date from a number of dates stored in an array. I can get this working with hardcoded dates, but not using PHP Variables, any ideas?

<?php
                $dates = array
                (
                    '0'=> date('Y-m-d', $Date1),
                    '1'=> date('Y-m-d', $Date2),
                    '2'=> date('Y-m-d', $Date3),
                    '3'=> date('Y-m-d', $Date4),
                    '4'=> date('Y-m-d', $Date5),
                    '5'=> date('Y-m-d', $Date6),
                    '6'=> date('Y-m-d', $Date7),
                    '7'=> date('Y-m-d', $Date8)
                );

                function closes($dates, $findate)
                {
                    $newDates = array();

                    foreach($dates as $date)
                    {
                        $newDates[] = strtotime($date);
                    }

                    sort($newDates);
                    foreach ($newDates as $a)
                    {
                        if ($a >= strtotime($findate)) return $a;
                    }
                    return end($newDates);
                }


                $values = closes($dates, date('Y-m-d'));

                ?>
                <h3>Next Date</h3><br/>
                <?php echo date('l, F jS, Y',$values); 

                }?>
Frog82
  • 464
  • 1
  • 8
  • 25
  • 1
    `strtotime()` returns an integer. Get the differences, `$diff[]=abs($a-$b)`, and then find the smallest number, `min($diff)`. You will have to keep track of which diff belongs to which date, but that is just bookkeeping. – Sverri M. Olsen Apr 15 '15 at 10:16
  • `'0'=> date('Y-m-d', $Date1),` should be `'0'=> date('Y-m-d', strtotime($Date1)),` – Shaunak Shukla Apr 15 '15 at 10:37
  • @deceze and mark-barker How is this a duplicate question? As mentioned (If you read the question) I have this working with Hardcoded values, like in that other question, but not with duplicate values and im simple trying to find out why this is the case.... – Frog82 Apr 15 '15 at 10:58

1 Answers1

0

can you please try this:

    <?php
    $day1 = '2015-01-01';
    $day2 = '2015-02-08';
    $day3 = '2015-03-13';
    $day4 = '2015-04-05';
    $day5 = '2015-05-27';
    $day6 = '2015-06-08';
    $day7 = '2015-07-19';  // YOU CAN ASSIGN DATE DYNAMICALLY

    $referenceDate = strtotime(date('Y-m-d')); // REFERENCE DATE TO BE COMPARED


    $dates = array(
                0 => strtotime($day1),
                1 => strtotime($day2),
                2 => strtotime($day3),
                3 => strtotime($day4),
                4 => strtotime($day5),
                5 => strtotime($day6),
                6 => strtotime($day7),
            ); // FORM AN ARRAY 

    foreach($dates as $date) {
        $diff = $date - $referenceDate;

        //echo '<pre>'; print_r($diff);

        if($diff > 0){
            $resultArrFuture[$diff] = date('d-m-Y', $date);
        } else {
            $resultArrPast[$diff] = date('d-m-Y', $date);
        }
    }       
    // THIS WILL GIVE YOU CLOSEST DATE IN TWO DIFFERENT ARRAY ONE IS FOR FUTURE DATE OF REFERENCE ARRAY AND ONE FOR PAST 


    ksort($resultArrFuture);
    krsort($resultArrPast);

    echo '<pre>'; print_r($resultArrFuture);
    echo '<pre>'; print_r($resultArrPast);

?>
Anto S
  • 2,448
  • 6
  • 32
  • 50