开发者

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 thin开发者_运维百科k 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!


Try strtotime().

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


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


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

  • https://stackoverflow.com/search?q=calculate+date+from+php
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜