Your Ad Here

Posted By

vitorbari on 12/15/10


Tagged

php date validates


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

AllKnightAccess


Increment Month


 / Published in: PHP
 

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')

  1. /**
  2.  *
  3.  * @param String $data 'yyyy-mm-dd' the base date
  4.  * @param Int $nmonth number of months to add
  5.  * @param String $format The output format
  6.  *
  7.  * @return String
  8.  **/
  9. function incrementsMonth($data, $nmonth = 1 , $format = 'Y-m-d')
  10. {
  11. $timeStampAddMonth = strtotime( " + $nmonth month " , strtotime( $data ) );
  12. $vetorData = explode('-',$data);
  13. $timeStampNovoMes = strtotime( date('Y-m-t', strtotime( " + $nmonth month " , mktime( 0,0,0,$vetorData[1],1,$vetorData[0] ))));
  14. return ($timeStampAddMonth > $timeStampNovoMes) ? date($format, $timeStampNovoMes) : date($format, $timeStampAddMonth) ;
  15. }

Report this snippet  

You need to login to post a comment.