What's wrong with this code?

0

Here is the code:

$aniversario = $_POST['aniversario'];
    $dataAtual = date("Y/m/dd");
    $noRobot = $dataAtual - $aniversario;
    echo $noRobot;

Here is the error:

  

Notice: A non-formed numeric value encountered in C: \ xampp \ htdocs \ toqve \ no_robot.php on line 18.

Thank you to respond.

    
asked by anonymous 04.10.2017 / 19:26

1 answer

5

You're making dates differently, as if they were numbers, and that's not right.

Make sure that both variables containing date are in the same format. Then use the DateTime class and make the difference between them.

Then try:

$post_aniversario = '11/05/2000';

$aniversario = new DateTime();
$aniversario = $aniversario->createFromFormat('d/m/Y', $post_aniversario);
$data_atual = new DateTime();
$diff = $aniversario->diff($data_atual);
print_r($diff); // or $diff->days

Result:

DateInterval Object (     [y] = > 0     [m] = > 0     [d] = > x     [h] = > 0     [i] = > 0     [s] = > 0     [invert] = > 0     [days] = > x )

Or even using datediff

$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);

Explanation date_diff

    
04.10.2017 / 19:31