mike
mike

Reputation: 8141

php, get an end date based on start date and number of months

I need to calculate an end date based on todays date and a number of months. For example, todays date 04/01/2010 and the number of months is 6. Are there any functions I can use to do some math that would return "10/01/2010"?

Also, I think it's important to note that I am storing the result in MySQL as a "DATETIME" so I guess I would have to do something with the time as well... ???

Thanks!

Upvotes: 0

Views: 302

Answers (3)

Gordon
Gordon

Reputation: 316969

Try

  • strtotime — Parse about any English textual datetime description into a Unix timestamp

Example

echo date('Y-m-d', strtotime('+6 months')); // 2010-10-01

See the API description for the various input formats. strotime also accepts a timestamp as the second argument that the first argument will be calculated relative to. If you don't set it, the timestamp for the current datetime will be used.

This has been asked and answered numerous times, so you might want to check out some of

Upvotes: 1

zombat
zombat

Reputation: 94147

If you are storing the result as a DATETIME in MySQL, then the following code is probably what you want:

$mysqlDate = date("Y-m-d H:i:s",strtotime("+6 months"));

strtotime() turns a string into a timestamp, and will work from the current time with a relative value by default. date() will use that timestamp, and the format string above will give you a MySQL-friendly TIMESTAMP value.

e.g.:

echo date("Y-m-d H:i:s",strtotime("+6 months"));
//outputs 2010-10-01 11:19:17

Upvotes: 1

pinaki
pinaki

Reputation: 5473

Try strtotime().

date('Y-m-d', strtotime("+n months"));

Upvotes: 2

Related Questions