note 92038 added to function.checkdate
| From: | taris83 at nate dot com | Date: | Mon, 06 Jul 2009 17:40:40 +0000 |
| Subject: | note 92038 added to function.checkdate | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-157976@lists.php.net to get a copy of this message | ||
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'
*/
----
Server IP: 211.43.210.124
Probable Submitter: 115.136.236.120
----
Manual Page -- http://www.php.net/manual/en/function.checkdate.php
Edit -- https://master.php.net/note/edit/92038
Del: integrated -- https://master.php.net/note/delete/92038/integrated
Del: useless -- https://master.php.net/note/delete/92038/useless
Del: bad code -- https://master.php.net/note/delete/92038/bad+code
Del: spam -- https://master.php.net/note/delete/92038/spam
Del: non-english -- https://master.php.net/note/delete/92038/non-english
Del: in docs -- https://master.php.net/note/delete/92038/in+docs
Del: other reasons-- https://master.php.net/note/delete/92038
Reject -- https://master.php.net/note/reject/92038
Search -- https://master.php.net/manage/user-notes.php