note 52199 added to function.strtotime
| From: | websafe at partybitchez dot org | Date: | Sun, 24 Apr 2005 06:53:39 +0000 |
| Subject: | note 52199 added to function.strtotime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-88464@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/function.strtotime.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52199
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52199&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52199&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52199&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52199&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52199&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52199&report=yes
Search -- http://master.php.net/manage/user-notes.php