0

Struggling to get my head around this...

$dates_list outputs this array

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [event_date] => 2020-11-19 00:00:00
                    [event_start_time] => 13:07:00
                    [event_end_time] => 17:07:00
                )

            [1] => Array
                (
                    [event_date] => 2020-06-17 00:00:00
                    [event_start_time] => 10:07:00
                    [event_end_time] => 17:07:00
                )

            [2] => Array
                (
                    [event_date] => 2020-03-05 00:00:00
                    [event_start_time] => 15:46:00
                    [event_end_time] => 20:46:00
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [event_date] => 2020-07-07 00:00:00
                    [event_start_time] => 20:10:00
                    [event_end_time] => 20:10:00
                )

            [1] => Array
                (
                    [event_date] => 2020-03-13 00:00:00
                    [event_start_time] => 20:10:00
                    [event_end_time] => 20:10:00
                )

        )

)

I've created $now which shows the current date and time

$now = date("Y-m-d h:i:s");
// 2020-03-12 09:17:42

@RoboRobok provided this answer, it worked when the array wasn't nested

$dates_filtered = array_filter($dates_list,
            function ($date_entry) use ($now) {
                return $date_entry['event_date'] >= $now;
            }
        );

How can I access the nested arrays to remove past days from the array?

1

2 Answers 2

1

Use array_filter:

$result = array_filter(
    $dates_list,
    function ($date_entry) use ($now) {
        $date = substr($date_entry['event_date'], 0, 10);
        $time = $date_entry['event_start_time'];

        return "$date $time" >= $now;
    }
);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! It might be useful if it could take the time into account aswell.
Updated to make use of event_start_time.
Lol. Why is this accepted answer? How can you use substr for date? This code won't work if the date format is different.
@RafiqueMohammed I believe it's sometimes important to make safe assumptions. This is one of these cases. Most database systems store their datetimes in this format and it's okay to assume that's the format to use. Remember, as I assume you're quite young programmer - sometimes simplicity is the way to go, but you need to be carefult with that for sure. Another thing - sometimes assumptions make your code safer, because you will get errors while parsing something, instead of allowing multiple formats. There might be more clients of your data, so the more consistent format, the better.
-1

Try this

foreach($dates_list as $i=>$dateItem){
   if (strtotime($dateItem['event_date']) < time()) {
    // past date
    unset($dates_list[$i]);
}

}
print_r($dates_list);

3 Comments

Thanks, this works but print_r($dates_list); displays all the foreach loops. Only the last loop shows the array with the past date removed @Robo Robok answer outputs just the array with the past date removed.
@StephenMeehan i just typed the code without testing. The reason of multiple print is bcz i added print_r inside the {..} bracket. I have updated the code. The array $dates_list will have updated dates without past dates. I wonder how u didn't figured it out? print_r use to print the array. i added it for demonstration purpose.
The downvote is pointless. The person should add the reason for downvoting.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.