note 66698 deleted from function.date by derick

From: Date: Sun, 11 Jun 2006 11:47:54 +0000
Subject: note 66698 deleted from function.date by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-114273@lists.php.net to get a copy of this message
Note Submitter: minterior at gmail dot com ---- 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; } ?>

« previous php.notes (#114273) next »