note 66698 added to function.date
| From: | minterior at gmail dot com | Date: | Fri, 26 May 2006 12:12:18 +0000 |
| Subject: | note 66698 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-112321@lists.php.net to get a copy of this message | ||
In my before post I was wrong. The function returns the number of months + 1 between two dates. For
example, if we compare "now" with "now", the result is 1 month. The same occurs
if dates are "01-01-2005" and "31-12-2006", it will return 24 and no 23 months.
The problem resides in the supposition that every month has the same number of seconds, and
it's not true. I've reprogrammed the function and now is based on strtotime function,
adding one month to the oldest date.
Enjoy it ;)
<?php
/**
* Calcula el número de meses entre dos fechas dadas
*/
function month_diff( $start_date, $end_date ) {
// dates can be in any format accepted by the strtotime function
// convert to UNIX timestamps
$start_date = strtotime( $start_date );
$end_date = strtotime( $end_date );
if ( $start_date < $end_date ) {
$date1 = $start_date;
$date2 = $end_date;
} elseif ( $start_date > $end_date ) {
$date1 = $end_date;
$date2 = $start_date;
} else return 0; // Dates are the same
$months = 0;
while ( strtotime("+$months month",$date1 ) <= $date2 ){
$months++;
}
return $months-1;
}
?>
----
Server IP: 130.206.1.5
Probable Submitter: 80.35.133.91
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- http://master.php.net/note/edit/66698
Del: integrated -- http://master.php.net/note/delete/66698/integrated
Del: useless -- http://master.php.net/note/delete/66698/useless
Del: bad code -- http://master.php.net/note/delete/66698/bad+code
Del: spam -- http://master.php.net/note/delete/66698/spam
Del: non-english -- http://master.php.net/note/delete/66698/non-english
Del: in docs -- http://master.php.net/note/delete/66698/in+docs
Del: other reasons-- http://master.php.net/note/delete/66698
Reject -- http://master.php.net/note/reject/66698
Search -- http://master.php.net/manage/user-notes.php