Search code examples
phpdatecomparison

How to collect dates with respect to specific date value in php?


I want to show a message for customer about expiry amount which is expiring this month, so that he can renew it. Renew time starts from the last month of that expiring amount, that is:

$renewDate = date("Y-m-d", strtotime("-1 month", $currentdate));

I have

Expiry date: Amount

2019-11-31: 15

2019-11-22: 20

2020-5-12: 30

2019-11-2: 10

How can I collect only those amounts which are not less than renew date and are within the range of last one month.


Solution

  • By playing with @Angel answer I figured it out:

      $arr = [
          0=>[
            'date' => '2019-12-1',
            'amount' => 15
          ],
          1=>[
            'date' => '2019-11-31',
            'amount' => 22
          ],
          2=>[
            'date' => '2019-11-27',
            'amount' => 22
          ],
            3=>[
            'date' => '2019-11-12',
            'amount' => 33
          ],
            4=>[
            'date' => '2019-10-2',
            'amount' => 6
          ],
        ];
    
        $data = [];
        foreach ($arr as $key => $value) {
           if(strtotime(date('Y-m-d')) <= strtotime($value['date']) && 
      strtotime(date('Y-m-d').'+1 month') >= strtotime($value['date']))
           {
              $data[] = $value;
           }
        }
        echo "<pre>";
        print_r($data);
        echo "<pre>";