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'));
精彩评论