简体   繁体   中英

find the closest next date from the array in php

I have this array and want to find the next closest date for 02/21/2019 and set and as next date on my page, any help or idea will be appreciated

 Array
    (
        [0] => 2019-02-17
        [1] => 02/09/2019
        [2] => 02/23/2019
        [3] => 02/18/2019
        [4] => 02/25/2019
        [5] => 03/04/2019
        [6] => 03/11/2019
        [7] => 03/18/2019
        [8] => 03/25/2019
        [9] => 04/01/2019
        [10] => 04/08/2019
    )

This is the solution I used for finding the closest next date for my program and it's working.

   $date = '02/21/2019';
   $allDates= array
    (
        '2019-02-17',
        '02/09/2019',
        '02/23/2019',
        '02/18/2019',
        '02/25/2019',
        '03/04/2019',
        '03/11/2019',
        '03/18/2019',
        '03/25/2019',
        '04/01/2019',
         '04/08/2019',
    );    
   function date_sort($a, $b) {
        return strtotime($a) - strtotime($b);
    }
    usort($allDates, "date_sort");
    foreach ($allDates as $count => $dateSingle) {
        if (strtotime($date) < strtotime($dateSingle))  {
            $nextDate = date('m-d', strtotime($dateSingle));
            break;
        }
    }
echo $nextDate;

Please see this solution. Hope this help

<?php 

$search = strtotime('02/21/2019');
$arrValues = array
    (
        '2019-02-17',
        '02/09/2019',
        '02/23/2019',
        '02/18/2019',
        '02/25/2019',
        '03/04/2019',
        '03/11/2019',
        '03/18/2019',
        '03/25/2019',
        '04/01/2019',
         '04/08/2019',
    );

    foreach($arrValues as $val) {


        $newArr[] = strtotime($val);


    }


    function getClosest($search, $arr) {
       $closest = null;
       foreach ($arr as $item) {
        if($search <= $item) {

          if ($closest === null || abs($search - $closest) > abs($item - $search)) {
             $closest = $item;
          }
          }

       }
       return $closest;
    }



    $closest = getClosest($search, $newArr);

    echo date('Y-m-d', $closest);



?>

Convert date to unix time and compare numbers. I didn't test it, just an idea...

Sort array elements in order then pick next element closest to you date (element)

I am assuming you can fix the format of array and make it same

$dates = array
                            (
                                '0'=> "2013-02-18",
                                '1'=> "2013-02-12",
                                '2'=> "2013-02-05",
                                '3'=> "2013-01-29",
                                '4'=> "2013-01-27"
                            );


                        $current = date("Y-m-d");
                        //$count = 0;
                        foreach($dates as $day)
                        {
                            //$interval[$count] = abs(strtotime($date) - strtotime($day));
                            $interval[] = abs(strtotime($current) - strtotime($day));
                            //$count++;
                        }                   
                        asort($interval);
                        $closest = key($interval);                       
                        echo trim($datarray[$closest]);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM