开发者

PHP get 31 days distance from starting date

How can I get what date it 开发者_运维知识库will be after 31 days starting with $startDate, where $startDate is a string of this format: YYYYMMDD.

Thank you.


strtotime will give you a Unix timestamp:

$date = '20101007';
$newDate = strtotime($date.' + 31 days');

you can then use date to format that into the same format, if that's what you need:

echo date('Ymd', $newDate);


If you're using PHP 5.3:

$date = new DateTime('20101007');
$date->add(new DateInterval('P31D'));
echo $date->format('Y-m-d');

The pre-5.3 date functions are lacking, to say the least. The DateTime stuff makes it much easier to deal with dates. http://us3.php.net/manual/en/book.datetime.php


Just a note that +1 month will also work if you want the same date on the next month and not 31 days exactly each time.


echo date('Y m d',strtotime('+31 Days'));
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜