Traverse arrays with dates and compare with the current date

Asked

Viewed 369 times

3

I need to create a method that compares in two or more arrays if the current date is greater than the due dates (date_paymentX) that exist in these arrays and returns how many occurrences there are.

Method:

public function payments()
{
    $date = date('Y-m-d'); // Retorna 2017-08-09

    $services = Service::all()->toArray();

    dd($services);

}

dd() displays:

array:2 [▼

0 => array:22 [▼
"id" => 1
"date" => "2017-08-09"
"date_payment1" => "2017-08-01"
"price1" => "500"
"payment1" => "Não"
"date_payment2" => "2017-08-15"
"price2" => "500"
"payment2" => "Não"
"date_payment3" => "2017-08-20"
"price3" => "500"
"payment3" => "Não"
"customer_id" => 1
"created_at" => "2017-08-09 10:52:12"
"updated_at" => "2017-08-09 12:28:31"
]

1 => array:22 [▼
"id" => 2
"date" => "2017-08-09"
"date_payment1" => "2017-08-01"
"price1" => "500"
"payment1" => "Não"
"date_payment2" => "2017-08-15"
"price2" => "500"
"payment2" => "Não"
"date_payment3" => "2017-08-15"
"price3" => "500"
"payment3" => "Não"
"customer_id" => 1
"created_at" => "2017-08-09 12:09:56"
"updated_at" => "2017-08-09 12:29:12"
]

]

In this example there are 2 past due payments. You would need the method to return this value(2).

  • I liked your answer. Better than mine to tell you the truth. You can answer your own question. And in my opinion yours is more performatic.

2 answers

2

Another way I ended up doing to solve the problem whether payment was made or not:

public static function payments()
{
    $date = date('Y-m-d'); 
    $services = Service::all()->toArray();
    $overdue = 0;
    for($i = 0; $i < count($services); $i++){
        $overdue = 0;
        foreach($services as $service) {
            for($n = 1; $n <= 3; $n++) {
                if(strtotime($date) > strtotime($service['date_payment'.$n]) && $service['payment'.$n] == "Não" ) {
                    $overdue++;
                }
            }                
        }
    }
    return $overdue;
}
  • Great script. Congratulations.

2


Simple as that...

I commented on the code so you understand and can modify if you need.

public function payments()
{
    $date = date('Y-m-d'); // Retorna 2017-08-09

    $services = Service::all()->toArray();

    $contagemDeVencimentos = 0; // quantidade de pagamentos vencidos

    for($x = 0; $x < count($services); $x++){

        foreach($services[$x] as $key => $dataPagamento ){

            // faz uma pesquisa e verifica se a chave atual tem esse inicio (date_payment)
            if(strstr($key, "date_payment")){
                // se a data do pagamento for menor que a data de comparação (hoje)...
                if(strtotime($dataPagamento) < strtotime($date)){
                    $contagemDeVencimentos++; // adiciona mais uma quantidade
                }
            }

        }

    }

    echo $contagemDeVencimentos; // mostra a quantidade final
}

EDITION

As new information indicated in the comments

public function payments()
    {
        $date = date('Y-m-d'); // Retorna 2017-08-09

        $services = Service::all()->toArray();

        $contagemDeVencimentos = 0; // quantidade de pagamentos vencidos

        for($x = 0; $x < count($services); $x++){

            $number = 0;
            foreach($services[$x] as $key => $info ){

                if(strstr($key, "date_payment")){
                    if(strtotime($info) < strtotime($date)){
                        $numberPayment = explode("date_payment", $key);
                        $number = $numberPayment[1];
                    }
                }

                if($key == "payment".$number){
                    $number = 0;
                    if($info == "Não")
                    $contagemDeVencimentos++;
                } 

            }

        }

        echo $contagemDeVencimentos; // mostra a quantidade
    }
  • 1

    That’s exactly what it was, thank you.

  • I forgot a detail, even if the date is expired but you have a "Yes" on any of the paymentX, don’t count.

  • @Marcelo tranquil. I will edit soon.

  • Thanks for the edition.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.