What’s wrong with this code?

Asked

Viewed 55 times

0

Here’s the code:

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

That is the error:

Notice: A non well Formed Numeric value encountered in C: xampp htdocs toqve no_robot.php on line 18.

Thank you to those who answer.

  • what’s on line 18?

  • you need to see the number format for subtraction

  • Related: https://answall.com/questions/33469/como-comparar-datas-em-php

  • Are 2 equal dates ?

  • Other link: https://answall.com/questions/57/howto calculate the difference%C3%A7a-between-two-dates/70

1 answer

4


You are making the difference of dates directly, as if they were numbers, and this is not correct.

Make sure that both variables containing date are in the same format. Then use the Datetime(date) 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

Upshot:

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

Or still using Datediff

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

Explanation date_diff

  • On line 18 you have $noRobot = $dateAtual - $birthday; and they are different dates. One comes from the form and the other is created by the current date. I did not understand your explanation #Leonogueira. I’m actually with the sign of least wanting to see the result enters the two dates. The code comes to give me the result, here in my case was 37, but it displays this error.

  • I changed my answer completely to try to be clearer.

  • Thank you @Leonogueira, but I copied and pasted your reply and it didn’t work either. Another error appeared. What I want to do is take the contents of the birthday variable that is sent from a form and subtract by the current date.

  • Try now, I entered the birthday date format. Change the format if necessary.

  • appears here Dateinterval Object ( [y] => 17 [m] => 4 [d] => 23 [h] => 0 [i] => 0 [s] => 0 [f] => 0.763454 [weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0 [invert] => 0 [days] => 6355 [special_type] => 0 [special_amount] => 0 [have_weekday_relative] => 0 [have_special_relative] => 0 )

  • Exchange $post_birthday = '11/05/2000'; for your $POST, this is a php object with complete difference of dates. y -> years, m -> months, d -> days, h -> hours, etc

Show 2 more comments

Browser other questions tagged

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