note 92038 deleted from function.checkdate by danbrown

From: Date: Mon, 06 Jul 2009 17:44:35 +0000
Subject: note 92038 deleted from function.checkdate by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-157977@lists.php.net to get a copy of this message
Note Submitter: taris83 at nate dot com ---- function getDateMoveTo( $date, $offset ) { $is_negative = !( $offset > 0 ); $date = ereg_replace( '[^0-9]', '', $date ); $year = (int)substr( $date, 0, 4 ); $month = (int)substr( $date, 4, 2); $day = (int)substr( $date, 6, 2 ); for ( $max_i = abs( $offset ), $i = 0; $i < $max_i; $i++ ) { $is_negative ? $day-- : $day++; if ( !checkdate( $month, $day, $year ) ) { if ( $is_negative ) { $day = 31; if ( --$month < 1 ) { $month = 12; $year--; } } else { $day = 1; if ( ++$month > 12 ) { $month = 1; $year++; } } while ( !checkdate( $month, $day, $year ) ) { $is_negative ? $day-- : $day++; } } } return sprintf( '%d%02d%02d', $year, $month, $day ); } /* [Usage] getDateMoveTo( '20090101', -1 ); // You will get '20081231' getDateMoveTo( '20081231', 1 ); // You will get '20090101' */

« previous php.notes (#157977) next »