note 51844 deleted from function.strtotime by derick

From: Date: Sun, 15 Jul 2007 12:49:27 +0000
Subject: note 51844 deleted from function.strtotime by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-128587@lists.php.net to get a copy of this message
Note Submitter: ldbkutty at yahoo dot com ---- I was trying to find a funtion that will let me add months to a date. I have tried $myDate = strtotime("+1 month",mktime(0,0,0,1,31,2004)); This returns 2004-03-02 but I wanted the result as 2004-02-29. So, I noticed there is no equilent function like setLenient as false and I wrote one. Hope this helps for someone. <?php function addMonthNoLenient($my_date, $add_month) { $day = date( "d", $my_date ); $month = date( "m", $my_date ); $year = date( "Y", $my_date ); $new_date = date( "Y-m-d", mktime( 0, 0, 0, $month + $add_month, $day, $year) ); $month_for_days = ( $month + $add_month ) % 12; if( $month_for_days == 0 ) { $month_for_days = 12; } if( $month_for_days != date( "m", mktime( 0, 0, 0, $month + $add_month , $day, $year ) ) ) { $new_year = date( "Y", mktime( 0, 0, 0, $month + $add_month, $day, $year ) ); $days_in_month = cal_days_in_month( CAL_GREGORIAN, $month_for_days, $new_year ); $new_date = date( "Y-m-d", mktime( 0, 0, 0, $month + $add_month, $days_in_month, $year ) ); } return $new_date; } ?> <? $your_date = mktime( 0, 0, 0, 1, 30, 2004 ); $months_to_add = 25; echo addMonthNoLenient( $your_date, $months_to_add ); ?>

« previous php.notes (#128587) next »