You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

  1. Invalid cutoff timestamp: 2018-12-31T024:00:00+00:00 has a typo (24 instead of a valid hour like 23), and using the start of 2019 is a cleaner cutoff point.
  2. 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 list2 example (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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:21:40