regosa
regosa

Reputation: 63

How to get the nearest expiration date,

I have this array string :

[
 {"expiration": "0000-00", "quantity": -50},
 {"expiration": "2023-02", "quantity": 100},
 {"expiration": "2022-03", "quantity": 50}
]

how to get the nearest expiration date, means here I need to get the third one 2022-03 and ignore others & also ignore the date that comes with 0000-00

my code:

$minDate = "";

foreach (json_decode($row['items']) as $item) {
    if ($item->expiration != "0000-00") {
        $minDate = min($item->expiration);
    }
}

dd($minDate);

Upvotes: 0

Views: 231

Answers (3)

Syscall
Syscall

Reputation: 19780

min(), require two values (or more) and return the lowest value. In your case you gave only one. Then, min() cannot compare strings.

You can use several arrays functions such as array_column(), array_filter(), sort() and reset() (Less verbose, but could be less efficient with large data) to achieve this :

$data = json_decode('[
 {"expiration": "0000-00", "quantity": -50},
 {"expiration": "2023-02", "quantity": 100},
 {"expiration": "2022-03", "quantity": 50}
]');

// extract 'expiration', remove '0000-00'
$data = array_filter(array_column($data, 'expiration'), fn($item) => $item != '0000-00');
sort($data);
var_dump(reset($data)); // string(7) "2022-03"

demo (3v4l.org)

Upvotes: 2

Richard   Housham
Richard Housham

Reputation: 1680

Here is another way.

<?php
        //Enter your code here, enjoy!

    $json = "[{\"expiration\": \"0000-00\", \"quantity\": -50},{\"expiration\": \"2023-02\", \"quantity\": 100},{\"expiration\": \"2022-03\", \"quantity\": 50}]";
    
    $array = json_decode($json);
    
    $currentDate = date_create_from_format('Y-m-d', date("Y-m-d"));
    $daysDiff = 365;
    $nearest = "";
    
    
    
    for($i = 0; $i<count($array); $i ++) {
        
        
        $v = $array[$i];
        
        
        $d = explode("-",$v->expiration);
        
       
        
       
        if(checkdate(intval($d[1]),1,intval($d[0]))) {
        
          
        
            $date =date_create_from_format('Y-m-d',$d[0]."-".$d[1]."-1");
            
            $dateDiff = (array)date_diff($currentDate, $date);
            
            
            
            if($dateDiff["days"] < $daysDiff ) {
                
               $daysDiff = $dateDiff["days"];
                
                $nearest = $v;
            }
            
        }
        
    }
    
    var_dump($nearest)
        
    ?>

Bit more low key (it's been a while since I've done php)

Upvotes: 1

Ishaque Javed
Ishaque Javed

Reputation: 406

You can try something like this: -

$arr = getNearestExp($row['items']);
print_r($arr);

/**
*To get the Nearest Expiration date
*@input json array of expiration & quantity as 
*per the question
*/
function getNearestExp($items){
    $items = json_decode($items);  // convert the items to json object
    $itemArr = [];

    foreach($items as $item) {
        // exclude the '0000-00' expiration
        if($item->expiration != '0000-00'){ 
            $itemArr[strtotime($item->expiration)] = (array)$item;
        }
        
    }

    // sort the out put array to ASC order to get the smallest value 1st
    ksort($itemArr);

    // Re indexing the array to proper index keys
    $itemArr= array_values($itemArr);

    // return the first value of the array
    return $itemArr[0];
}

Upvotes: 2

Related Questions