-1

i have array

array(5) { 
    [0]=> string(19) "2012-06-11 08:30:49" 
    [1]=> string(19) "2012-06-07 08:03:54" 
    [2]=> string(19) "2012-05-26 23:04:04" 
    [3]=> string(19) "2012-05-27 08:30:00" 
    [4]=> string(19) "2012-06-08 08:30:55" 
}

i want to slice into array <= 2012-06-07 08:30:49

how to get those array?

Sira Naid
  • 17
  • 3

2 Answers2

1

You can use the builtin-fuction array_filter for this. The first argument is the array you want to use and the second is the callback for each iteration (each item in the array). We only want dates for each item that <= 2012-06-07 08:30:49 and those items that meets that critera is returned into the final $result - variable.

$result = array_filter($arr, function($data_item) {
    return $data_item <= "2012-06-07 08:30:49";
});

For more information look about the array_filter() look at: https://www.php.net/manual/en/function.array-filter.php

bestprogrammerintheworld
  • 5,417
  • 7
  • 43
  • 72
  • Are you sure that comparing __dates__ like __strings__ will work as expected? – u_mulder May 22 '20 at 07:10
  • @u_mulder - I have tested serveral different dates and times. And I got the expected result - so yes. (It worked in this case anyway). Please tell me if there is an issue with this! (I want to know in that case) – bestprogrammerintheworld May 22 '20 at 07:13
  • 1
    @NicoHaase - I thought it was kind of self explanatory but I guess you can never get to detailed. I have supplied a small text how it works. – bestprogrammerintheworld May 22 '20 at 09:25
1
$dates = ["2012-06-11 08:30:49",
        "2012-06-07 08:03:54",
        "2012-05-26 23:04:04",
        "2012-05-27 08:30:00",
        "2012-06-08 08:30:55",];
$filtered_array=array();
foreach ($dates as $value) {
    if($value <= "2012-06-07 08:30:49"){
        array_push($filtered_array, $value);
    }
}

print_r($filtered_array);

result:

Array ( [0] => 2012-06-07 08:03:54 [1] => 2012-05-26 23:04:04 [2] => 2012-05-27 08:30:00 )