note 83431 deleted from function.date by bjori
| From: | bjori@php.net | Date: | Mon, 26 May 2008 11:30:37 +0000 |
| Subject: | note 83431 deleted from function.date by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-139788@lists.php.net to get a copy of this message | ||
Note Submitter: jeremy at teratask dot com
Reason: useless
----
The date calculations designed to find the number of days between 2 dates do not fully account for
the start and end date. This will:
<?php
function numDays($date_1,$date_2) {
$start_day = strtotime(date('m/d/Y 00:00:00',strtotime($date_1)));
$end_day = strtotime(date('m/d/Y 23:59:59',strtotime($date_2)));
$difference = abs($end_day - $start_day);
//Shouldn't need to ceil() the return, but just in case microsecond fluctuations must be
accounted for
return ceil($difference/(86400));
}
?>
To calculate the number of months between 2 dates, this should help:
<?php
function numMonths($date_1, $date_2) {
$date_one = strtotime($date_1);
$date_two = strtotime($date_2);
$smallest_date = min($date_one,$date_two);
$smallest_year = date('Y',$smallest_date);
$smallest_month = date('m',$smallest_date);
$largest_date = max($date_one,$date_two);
$largest_year = date('Y',$largest_date);
$largest_month = date('m',$largest_date);
$years_between = $largest_year - $smallest_year - 1;
$months_from_smallest = 13 - $smallest_month;
if ($largest_year == $smallest_year) {
return $largest_month - $smallest_month + 1;
} else {
return $years_between*12 + $months_from_smallest + $largest_month;
}
}
?>