note 55268 deleted from function.strtotime by derick

From: Date: Sun, 15 Jul 2007 12:47:19 +0000
Subject: note 55268 deleted from function.strtotime by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-128577@lists.php.net to get a copy of this message
Note Submitter: faress at NOSPAMMING dot PLEASE dot gmail dot com ---- This is an quick correction to ldbkutty at yahoo dot com 12-Apr-2005 06:21 he was trying to find a function that will let him add months to a date. - 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 equivalent function like setLenient as false and he wrote one that I corrected a bit because my installation did not have the cal_days_in_month() function (I used the date('t', $timestamp) to get the days in months for a particular year) . Hope this helps someone. <?php function addMonthNoLenient($my_date, $add_month) { $mid_month = 15 ; $day = date( "d", $my_date ); $month = date( "m", $my_date ); $year = date( "Y", $my_date ); $hour = date( "H", $my_date ); $minute = date( "i", $my_date ); $second = date( "s", $my_date ); $new_date_timestamp = mktime( $hour, $minute, $second, $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( $hour, $minute, $second, $month + $add_month , $day, $year ) ) ) { $new_year = date( "Y", mktime( $hour, $minute, $second, $month + $add_month, $day, $year ) ); $days_in_month = date('t', mktime( $hour, $minute, $second, $month_for_days, $mid_month, $new_year ) ); $new_date_timestamp = mktime( $hour, $minute, $second, $month + $add_month, $days_in_month, $year ); } return $new_date_timestamp; } ?> <? $your_date = mktime( 0, 0, 0, 1, 30, 2004 ); $months_to_add = 25; echo date( "Y-m-d", addMonthNoLenient( $your_date, $months_to_add )); ?>

« previous php.notes (#128577) next »