Remove late date

Asked

Viewed 51 times

0

Good evening, I’m making a script that pulls the coupon number for my clients

  $cu = $_GET['bin'];

$bin = substr($cu, 0, 6);
$file = 'db.txt';
$searchfor = $bin;
$contents = file_get_contents($file);
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*\$/m";
if (preg_match_all($pattern, $contents, $matches)) {
    $encontrada = implode("<br>", $matches[0]);
}

echo $encontrada;

In this simple code it returns me all the coupons available starting with the numbering I reported on GET:

http://localhost/cli.php?bin=544828
544828|01|2019|018 
544829|09|2018|775 
544825|05|2018|036 
544822|10|2014|001 
544828|11|18|279 
544828|07|20|976 
544828|08|20|725 
544828|04|21|201 

I was wondering if my script can check the expiration year of all coupons and just print the ones that are not expired on the screen. Type:

    if(... < 2018 or ... < 18) {
Não printa na tela
}

Thank you

var_dump($matches[0]); //me retorna
array(125) { [0]=> string(29) "547874|09|2016|156 " [1]=> string(29) "5478749|10|2016|084 " [2]=> string(29) "547874902|12|2016|462 " [3]=> string(29) "54787490|05|2016|895 " [4]=> string(29) //e muito mais cupons...

The contents of db.txt are (several coupons):

5448285001|05|2018|456
544828500|05|2018|456
544828500175|05|2018|100
54482850017|01|2019|093
  • What is the column of the year? the third ? What is the information on 'db.txt' ? What gives var_dump($matches[0]); within the if ?

  • The column of the year is the third, on the other doubts I will change my topic.

  • Ready friend :) If you can take a look there

  • But those years like 18 sane 2018 ? Coupons that are not expired would be those that have year and month less or equal to the current ?

1 answer

1


You can do it this way, using foreach (explanations in the code):

$cu = $_GET['bin'];

$bin = substr($cu, 0, 6);
$file = 'db.txt';
$searchfor = $bin;
$contents = file_get_contents($file);
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*\$/m";
if (preg_match_all($pattern, $contents, $matches)) {

   foreach($matches[0] as $key => $valor){
      $t_array =  explode("|", $valor); // quebra pela barra vertical "|"
      $mes = $t_array[1]; // pega o segundo valor da array (mês)
      $ano = (int) substr($t_array[2], -2); // pega os dois últimos caracteres do terceiro valor da array (ano) e converte em inteiro

      $ano_atual = (int) substr(date("Y"), -2); // pega o dois últimos caracteres do ano atual e converte em inteiro

      // faz as comparações:
      // se o ano for maior ou igual que o ano atual e o mês for maior ou igual que o mês atual
      // ou se o ano for maior que o atual
      if( ($ano >= $ano_atual && $mes >= date("m")) || $ano > $ano_atual ){
         $encontrada .= $valor."<br>"; // concatena os valores
      }
   }

}

echo $encontrada; // imprime o resultado

Considering the values below, will return those marked with "OK":

544828|01|2019|018 -> OK: janeiro de 2019
544829|09|2018|775 -> OK: setembro de 2018
544825|05|2018|036
544822|10|2014|001
544828|11|18|279   -> OK: novembro de 2018
544828|07|20|976   -> OK: julho de 2020
544828|08|20|725   -> OK: agosto de 2020
544828|04|21|201   -> OK: abril de 2021

See example in Ideone

Clean code (no comments):

$cu = $_GET['bin'];

$bin = substr($cu, 0, 6);
$file = 'db.txt';
$searchfor = $bin;
$contents = file_get_contents($file);
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*\$/m";
if (preg_match_all($pattern, $contents, $matches)) {

   foreach($matches[0] as $key => $valor){
      $t_array =  explode("|", $valor);
      $mes = $t_array[1];
      $ano = (int) substr($t_array[2], -2);
      $ano_atual = (int) substr(date("Y"), -2);
      if( ($ano >= $ano_atual && $mes >= date("m")) || $ano > $ano_atual ){
         $encontrada .= $valor."<br>";
      }
   }

}

echo $encontrada;
  • 1

    Thank you very much, friend.

Browser other questions tagged

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