note 104864 added to function.date-add
| From: | knyrii at gmail dot com | Date: | Tue, 12 Jul 2011 16:45:41 +0000 |
| Subject: | note 104864 added to function.date-add | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-181438@lists.php.net to get a copy of this message | ||
A function I use for fixing date ranges. Can also be used to create a new date by subtracting or
adding days, months, or years. The resulting date will always be a valid date.
<?php
/**
* Fixes the date so that it is valid. Can handle negative values. Great for creating date ranges.
* NOTE: Changes the passed variables' values.
* Ex. 2-29-2001 will become 3-1-2001
* 15-6-2011 will become 3-6-2012
* 3-0-2001 will become 2-28-2001
* -10-29-2002 will become 3-1-2001
* @param integer $month 0 values decrement a year and is set to 12.
* @param integer $day 0 values decrement a month and is set to the last day of that month.
* @param integer $year
* @param boolean $unix Optional. Default is true. Use UNIX date format
* @return string The resulting string
*/
function date_fix_date(&$month,&$day,&$year,$unix=true){
if($month>12){
while ($month>12){
$month-=12;//subtract a $year
$year++;//add a $year
}
} else if ($month<1){
while ($month<1){
$month +=12;//add a $year
$year--;//subtract a $year
}
}
if ($day>31){
while ($day>31){
if ($month==2){
if (is_leap_year($year)){//subtract a $month
$day-=29;
} else{
$day-=28;
}
$month++;//add a $month
} else if (date_hasThirtyOneDays($month)){
$day-=31;
$month++;
} else{
$day-=30;
$month++;
}
}//end while
while ($month>12){ //recheck $months
$month-=12;//subtract a $year
$year++;//add a $year
}
} else if ($day<1){
while ($day<1){
$month--;//subtract a $month
if ($month==2){
if (is_leap_year($year)){//add a $month
$day+=29;
}else{
$day+=28;
}
} else if (date_hasThirtyOneDays($month)){
$day+=31;
} else{
$day+=30;
}
}//end while
while ($month<1){//recheck $months
$month+=12;//add a $year
$year--;//subtract a $year
}
} else if ($month==2){
if (is_leap_year($year)&&$day>29){
$day-=29;
$month++;
} else if($day>28){
$day-=28;
$month++;
}
} else if (!date_hasThirtyOneDays($month)&&$day>30){
$day-=30;
$month++;
}
if ($year<1900) $year=1900;
if ($unix){
return "$year-$month-$day";
} else{
return "$month-$day-$year";
}
}
/**
* Checks to see if the month has 31 days.
* @param integer $month
* @return boolean True if the month has 31 days
*/
function date_hasThirtyOneDays($month){
//1234567 89012:1357 802
//JfMaMjJ AsOnD:JMMJ AOD
if ($month<8)
return $month%2==1;
else
return $month%2==0;
}
/**
* Checks to see if the year is a leap year.
* @param integer $year
* @return boolean True if the year is a leap year
*/
function is_leap_year($year){
return (0 ==$year%4&&0!=$year%100 || 0 ==$year%400);
}
?>
----
Server IP: 208.69.120.55
Probable Submitter: 68.101.56.168
----
Manual Page -- http://www.php.net/manual/en/function.date-add.php
Edit -- https://master.php.net/note/edit/104864
Del: integrated -- https://master.php.net/note/delete/104864/integrated
Del: useless -- https://master.php.net/note/delete/104864/useless
Del: bad code -- https://master.php.net/note/delete/104864/bad+code
Del: spam -- https://master.php.net/note/delete/104864/spam
Del: non-english -- https://master.php.net/note/delete/104864/non-english
Del: in docs -- https://master.php.net/note/delete/104864/in+docs
Del: other reasons-- https://master.php.net/note/delete/104864
Reject -- https://master.php.net/note/reject/104864
Search -- https://master.php.net/manage/user-notes.php