Link PHP to HTML and Calculate how old and months

Asked

Viewed 77 times

0

I would like to know if it is possible and how I can link this html in php, adding the count of how many years and how many months a person has

I also wanted a php function that shows the name when sent

I have this function for the name

  <?php
    
    if(isset($_post['enviar])){
         $nome= $_POST['nome'];
    
         echo *Seu nome é $nome !*;
    }

?>

HTML:

 <label for="input">Qual seu nome? </label>
         <br/> <br/>
        <input type="text" id=valor> 
        <input type="submit" onclick="capturar()" value="Enviar"> <br/>
        <p id="valorDigitado"></p>
     
        
    <label for="input">Qual a data do seu nascimento? </label> <br/> <br/>
        <input type="number" id="date" placeholder="data"> <br/>
        <input type="number" id="month" placeholder="mês"> <br/>
        <input type="number" id="year" placeholder="ano"> <br/>
        <br/> <br/>
    
        <button id="calc-btn">Calcular Idade</button>
    
        <p id="show-age"></p>  

PHP if the date entered is 09/01/1997

<?php
    //Data atual
    $dia = date (‘d’);
    $mes = date (‘m’);
    $ano = date (‘Y’);
    //Data do aniversário
    $dianasc = (’09′);
    $mesnasc = (’01′);
    $anonasc = (’1997′);
    //Calculando sua idade
    $idade = $ano - $anonasc;
    if ($mes < $mesnasc){
    $idade-;
    echo “$idade”;
    }
    elseif ($mes == $mesnasc and $dia <= $dianasc) {
    $idade-;
    echo “$idade”;
    }
    else{
    echo “$idade”;
    }
    ?>
  • Why not do everything in javascript? Vc know when and pq use one or the other?

  • 1

    This answers your question? Show age in years in php

  • tvdias, need to do in php :(

  • But the answers to the other question are in PHP, no?

1 answer

1


You need a form, in the form action pass the name of the php file (data.php). The name of the html input fields are the keys in the $_POST['name'] and $_POST['data'] variable. O e explodes ("-",$data); divides the date based on "-" and returns an array where position 0 is the year, position 1 is month and position 2 is the day. This is the date format that goes from input to php 2021-01-31.

data php.

<?php
$nome = $_POST['nome'];
$data =  $_POST['data'];
$diasD = explode("-",$data);
//Data atual
$dia = date ('d');
$mes = date ('m');
$ano = date ('Y');
//Data do aniversário
$dianasc = ($diasD[2]);
$mesnasc = ($diasD[1]);
$anonasc = ($diasD[0]);
 //Calculando sua idade
 $idade = $ano - $anonasc;
if ($mes < $mesnasc){
    $idade -= 1;
    echo "$idade";
}
elseif ($mes == $mesnasc and $dia <= $dianasc) {
    $idade -= 1;
    echo "$idade";
}
else{
    echo "$idade";
}
?>

index.html

<form action="data.php" method="post">
   <input type="text" name="nome" id="nome">
   <input type="date" name="data" id="data">
   <input type="submit">
</form>
  • very good, vlw!

Browser other questions tagged

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