note 52199 deleted from function.strtotime by derick

From: Date: Sun, 15 Jul 2007 12:48:54 +0000
Subject: note 52199 deleted from function.strtotime by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-128585@lists.php.net to get a copy of this message
Note Submitter: websafe at partybitchez dot org ---- A small update... Should be ok now. <?php function count_months($start_date, $end_date) { # Returns whole-month-count between two dates # websafe<at>partybitchez<dot>org # usage # count_months("1978-02-18", "2028-08-12"); # $start_date_unixtimestamp = strtotime($start_date); $start_date_month = date("m", $start_date_unixtimestamp); $end_date_unixtimestamp = strtotime($end_date); $end_date_month = date("m", $end_date_unixtimestamp); $calculated_date_unixtimestamp = $start_date_unixtimestamp; $counter=0; while ($calculated_date_unixtimestamp < $end_date_unixtimestamp) { $counter++; $calculated_date_unixtimestamp = strtotime($start_date . " +{$counter} months"); } # if ( ($counter==1) && ($end_date_month==$start_date_month)) $counter=($counter-1); return $counter; } ?> Example 01: count_months("1996-02-18", "2005-11-24") returns 118 Example 02: count_months("2005-04-24", "2005-04-24") returns 0 Example 03: count_months("2005-04-24", "2005-04-25") returns 0 Example 04: count_months("1978-02-18", "2028-08-12") returns 606 Example 05: count_months("2008-02-29", "2009-02-29") returns 12 Example 06: count_months("2008-02-29", "2008-03-01") returns 1 Example 07: count_months("2008-02-29", "2008-03-02") returns 1 Example 08: count_months("2008-02-29", "2009-02-28") returns 12 Example 09: count_months("2008-02-29", "2009-02-29") (not existing enddate) returns 12 Example 10: count_months("2008-02-29", "2009-03-01") returns 12 Example 11: count_months("2008-02-29", "2009-03-02") returns 13

« previous php.notes (#128585) next »