Return to Snippet

Revision: 37718
at December 15, 2010 23:36 by vitorbari


Initial Code
/**
 * 
 * @param String $data 'yyyy-mm-dd' the base date
 * @param Int $nmonth number of months to add
 * @param String $format The output format
 *
 * @return String
 **/
function incrementsMonth($data, $nmonth = 1 , $format = 'Y-m-d')
{	
	$timeStampAddMonth = strtotime( " + $nmonth month " , strtotime( $data ) );
	$vetorData = explode('-',$data);
	$timeStampNovoMes =  strtotime( date('Y-m-t', strtotime( " + $nmonth month " ,   mktime( 0,0,0,$vetorData[1],1,$vetorData[0] ))));
	return ($timeStampAddMonth > $timeStampNovoMes) ? date($format, $timeStampNovoMes) : date($format, $timeStampAddMonth) ;
}

Initial URL

                                

Initial Description
Use this function if you need to get the same day in the next month (or how many you need).
Using defaults functions of php you got this:
echo date('Y-m-d', strtotime( " + 1 month " ,  strtotime('2010-01-31')));  (Output: '2010-03-03')

and if you use the function 'incrementsMonth', you got :
echo incrementsMonth('2010-01-31');  (Output: '2010-02-28')

Initial Title
Increment Month

Initial Tags
php, date

Initial Language
PHP