PHP筛选无2019年操作记录的列表问题求助
How to Filter Lists with No 2019 Actions
Let's fix this step by step. Your goal is to keep only lists where every single action happened before January 1st, 2019 UTC—meaning no actions in 2019 at all. Here's what's wrong with your current approach and how to fix it:
Issues in Your Current Code
- Invalid cutoff timestamp:
2018-12-31T024:00:00+00:00has a typo (24instead of a valid hour like23), and using the start of 2019 is a cleaner cutoff point. - Incorrect logic: You're only echoing actions that are after the cutoff, but you need to track whether the entire list contains any 2019 actions to decide if it should be kept.
Corrected Approach Using DateTime Objects
Using PHP's DateTime class is far more reliable for time comparisons than string checks—it handles time zones correctly and avoids edge cases. Here's a complete solution:
// Define the cutoff: start of January 1st, 2019 UTC $cutoff = new DateTime('2019-01-01T00:00:00+00:00'); // Example array of all your lists $allLists = [ 'list1' => [ 'list' => [ ['action' => 'open', 'timestamp' => '2019-04-18T18:03:24+00:00'], ['action' => 'click', 'timestamp' => '2018-12-18T18:03:24+00:00'] ] ], 'list2' => [ 'list' => [ ['action' => 'open', 'timestamp' => '2018-04-18T18:03:24+00:00'], ['action' => 'open', 'timestamp' => '2018-03-01T18:03:24+00:00'] // Fixed invalid date format here ] ] ]; $validLists = []; foreach ($allLists as $listName => $listData) { $has2019Action = false; // Check each entry in the list foreach ($listData['list'] as $entry) { $entryTime = new DateTime($entry['timestamp']); // If any entry is on/after 2019, mark the list as invalid if ($entryTime >= $cutoff) { $has2019Action = true; break; // No need to check further entries } } // If no 2019 actions exist, keep the list if (!$has2019Action) { $validLists[$listName] = $listData; echo "Valid list: $listName\n"; print_r($listData); } }
Key Details
- Early termination: As soon as we find a 2019 action in a list, we stop checking the rest of its entries—this saves processing time.
- Valid date handling: Fixed the typo in your
list2example (2018-03-1→2018-03-01) to ensure the timestamp is parseable. - Flexibility: If you're processing lists one at a time instead of in a batch, you can adapt the inner loop logic to check individual lists directly.
For Individual List Checks
If you're working with a single list instead of a batch, use this simplified version:
$currentList = [ 'list' => [ ['action' => 'open', 'timestamp' => '2018-04-18T18:03:24+00:00'], ['action' => 'open', 'timestamp' => '2018-03-01T18:03:24+00:00'] ] ]; $cutoff = new DateTime('2019-01-01T00:00:00+00:00'); $has2019Action = false; foreach ($currentList['list'] as $entry) { $entryTime = new DateTime($entry['timestamp']); if ($entryTime >= $cutoff) { $has2019Action = true; break; } } if (!$has2019Action) { echo "This list is valid (no 2019 actions):\n"; print_r($currentList); } else { echo "This list contains 2019 actions and will be excluded.\n"; }
内容的提问来源于stack exchange,提问作者Johny
相关产品推荐
相关产品推荐

