Sum int with date - PHP

Asked

Viewed 206 times

0

I have the function below, which makes the date sum, with the value +2 days fixed:

echo date('d/m/Y', strtotime("+2 days",strtotime($ultima_data)));

It sums correctly with the value of $last date, but somehow need to change the +2 days for a value that is in another variable, but so far could not. I tried the forms below, unsuccessfully:

echo date('d/m/Y', strtotime($dias_frequencia,strtotime($ultima_data)));

echo date('d/m/Y', strtotime("+'$ultima_frequencia'days",strtotime($ultima_data)));

In both cases there was no error, but returned values 1969... Any idea?

  • 3

    http://answall.com/q/120381/91 and remove those single quotes there.

  • 2

    You missed a spot here: "+'$ultima_frequencia'days" wouldn’t be like this "+'$ultima_frequencia' days"?

  • @rray was worth again. Another one I will write in the notebook kkk. Guilherme Nascimento changed everything to double quotes, as in the question that rray sent and worked. Thanks for the answers!

  • 1

    Oh it’s true, I didn’t even notice it =)

  • 1

    I understand that it has been a long time dear Diego, yet I think it deserved a technical answer: https://answall.com/a/324727/3635 :)

2 answers

2

I know that so far the question has probably been solved, I came to comment on the lack of space:

You missed a spot here: "+'$ultima_frequencia'days" wouldn’t be like this "+'$ultima_frequencia' days"?

But I had forgotten to mention also the unnecessary apostrophes, I mean if I do this:

 echo date('d/m/Y', strtotime("+'10' days"));

It will return 1969 or 1970, something like will be shown:

01/01/1970

Now doing so works:

 echo date('d/m/Y', strtotime("+10 days"));

In the case today being 24/08/2018 I was shown this (this month ends on 31):

03/09/2018

In other words, the problem is not concatenating, actually with double quotes it is not always necessary to concatenate and it does not cause problem of legibility to use variables inside these quotes, you can even make more visible using something like:

 echo "Olá $nome, você recebeu {$mail->mailbox[0]->messages}";

What from my point of view is as legible as concatenating:

 echo "Olá " . $nome . ", você recebeu " . $mail->mailbox[0]->messages;

0

do so:

echo date('d/m/Y', strtotime("+" . $ultima_frequencia  . " days",strtotime($ultima_data)));

Concatenating, your code becomes more readable.

Browser other questions tagged

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