Travers arrays with dates and compare to the current date

3

I need to create a method that compares two or more arrays if the current date is greater than the due date (date_paymentX) that exists in those 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 overdue payments. You would need the method to return this value (2).

    
asked by anonymous 09.08.2017 / 17:41

2 answers

2

Simple like this ...

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

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

According to 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
    }
    
09.08.2017 / 18:27
2

Another way I ended up doing this was whether the payment was done:

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;
}
    
10.08.2017 / 02:45