note 51844 added to function.strtotime

From: Date: Tue, 12 Apr 2005 22:21:57 +0000
Subject: note 51844 added to function.strtotime
Groups: php.notes 
Request: Send a blank email to php-notes+get-87914@lists.php.net to get a copy of this message
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 ); ?> ---- Manual Page -- http://www.php.net/manual/en/function.strtotime.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+51844 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51844&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51844&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51844&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51844&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51844&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+51844&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#87914) next »