How to assign 60 more days in a date compared to today’s PHP date?

Asked

Viewed 566 times

4

I am assigning today’s date in a variable, I would like to know how to assign today’s date plus 60 days. How would the syntax ?

$dateIni = date('Y-m-d');

1 answer

6


You can pass a second argument to date to specify the date, through a timestamp. In this case, we will use strtotime to facilitate the service (this function creates a string-based timestamp that represents the desired amount of time).

$dateIni = date('Y-m-d', strtotime('+60 days'));

It is also possible to do through the class DateTime - which I always prefer to use.

Behold:

$date = new DateTime('+60 days');

echo $date->format('Y-m-d');

If you are using PHP 5.4 >=, you can still do so:

 echo (new DateTime('+60 days')->format('Y-m-d');

To fix the knowledge about dates in PHP, I’ll link some important questions on the subject:

Browser other questions tagged

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