Pick up date with a function

Asked

Viewed 38 times

0

I need help with the following:

Method to get the date


function pegar_ano(){

   $year = date('Y');
   $ano = date('Y');
   //echo $year;
   $ano_anterior = $_POST['ano_anterior'];
   $ano_atual= $_POST['ano_atual'];
   $ano_posterior= $_POST['ano_posterior'];
   if($ano_anterior != " "){
   $year = $ano_anterior;
   }
   if($ano_posterior != " "){
   $year = $ano_posterior;
   }
   if($ano_atual != " "){
   $year = $ano_atual;
   }
   return $year;
   }

Form in html

<form method="post" action="">
        <br><br>

        <button type="submit" class="button1" name="ano_anterior" value="<?php echo $year - 1; ?>" style="padding: 5px 20px;"> Ano Anterior </button>
        <button type="submit" class="button1" name="ano_atual" value="<?php echo date("Y") ; ?>" style="padding: 5px 20px;"> Ano Atual </button>
        <button type="submit" class="button1" name="ano_posterior" value="<?php echo $year + 1; ?>" style="padding: 5px 20px;"> Ano Posterior </button>

    </form>

Inside of another Function

It would put the peg_year inside another Function to build the date, then make the query in sql

 $data = pegar_ano().'-'.$mes.'-'.$a.$diacorrente;
  • But I don’t understand, what is the doubt?

  • The function is not picking up the POSTS to make the conditions @Mikedoouglas

1 answer

0

I’m not sure about your question. But from what I understand you need two things:

1) Missing calling function on: <?php echo $year; ?>, should stay <?php echo pegar_ano();?>

What you are doing is calling a variable ($year) not the function.

2) For the function to understand $_POST you need to pass it to function. Thus it should be:

   function pegar_ano($post){

   $year = date('Y');
   $ano = date('Y');
   //echo $year;
   $ano_anterior = $post['ano_anterior'];
   $ano_atual= $post['ano_atual'];
   $ano_posterior= $post['ano_posterior'];
   if($ano_anterior != " "){
   $year = $ano_anterior;
   }
   if($ano_posterior != " "){
   $year = $ano_posterior;
   }
   if($ano_atual != " "){
   $year = $ano_atual;
   }
   return $year;
   }

In addition you need to pass this information to function, soon, pegar_ano() should receive the posts getting pegar_ano($_POST);

Browser other questions tagged

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