Difference between 2 dates -- know something better?
05-13-2008, 01:25 AM
|
Difference between 2 dates -- know something better?
|
Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
|
I needed to find the number of months between 2 dates. I don't know of a function in PHP, so I wrote this.
PHP Code:
<?php define('DATE_MONTH',1); define('DATE_DAY',2);
function dateDifference($date_1, $date_2, $type=DATE_MONTH) { $start_date = strtotime($date_1); $end_date = strtotime($date_2); $sign = ((int)($end_date-$start_date)/abs($end_date-$start_date));
$period_offset = 0; if ($sign > 0) { switch ($type) { case DATE_MONTH: while (strtotime('+'.$period_offset.' months',$start_date) <= $end_date) { $period_offset++; } break; case DATE_DAY: while (strtotime('+'.$period_offset.' days',$start_date) <= $end_date) { $period_offset++; } break; } } else { switch ($type) { case DATE_MONTH: while (strtotime('-'.$period_offset.' months',$start_date) <= $end_date) { $period_offset++; } break; case DATE_DAY: while (strtotime('-'.$period_offset.' days',$start_date) <= $end_date) { $period_offset++; } break; } } return $period_offset; } echo dateDifference('5/1/2008','11/1/2008'); ?>
Anyone know of a better way?
__________________
Jeremy Miller
Please login or register to view this content. Registration is FREE
Last edited by JeremyMiller; 05-13-2008 at 01:46 AM..
Reason: Fixed code bug
|
|
|
|
05-13-2008, 03:20 AM
|
Re: Difference between 2 dates -- know something better?
|
Posts: 2,787
Name: Matt
Location: Irvine, CA
|
I'm not in a position where I can test this myself but it makes since in my head:
PHP Code:
function numberOfMonths($date1, $date2)
{
$diff = abs(strtotime($date2) - strtotime($date1));
$month = date('n', $diff);
$year = date('o', $diff);
$deltaYears = $year - 1970;
$deltaMonths = $deltaYears * 12 + ($month - 1);
return $deltaMonths;
}
I might be missing something but I think that will work, provided you don't use any dates prior to 1970. If it is the case that you are working with dates before 1970 just add the difference to each date (ie 1968 and 1969 become 1970 and 1971)
Last edited by NullPointer; 05-13-2008 at 03:31 AM..
Reason: Forgot to subtract 1 from the month
|
|
|
|
05-13-2008, 03:49 AM
|
Re: Difference between 2 dates -- know something better?
|
Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
|
Thanks. That code doesn't actually work b/c of how $diff is calculated (it's meaning is distorted for the date function. I see your difference in logic, however, and recoded it like this:
PHP Code:
<?php function numberOfMonths($date1, $date2) { $diff = abs(strtotime($date2) - strtotime($date1)); $month = date('n', $diff); $year = date('o', $diff); $deltaYears = $year - 1970;
$deltaMonths = $deltaYears * 12 + ($month - 1); return $deltaMonths; } echo numberOfMonths('11/15/2008','2/1/2009').'<br /><br />';
function numMonths($date_1, $date_2) { $smallest_date = min(strtotime($date_1),strtotime($date_2)); $smallest_year = date('Y',$smallest_date); $smallest_month = date('m',$smallest_date);
$largest_date = max(strtotime($date_1),strtotime($date_2)); $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; } } echo numMonths('11/15/2008','2/1/2009').'<br />'; echo numMonths('2/15/2008','11/1/2008').'<br />'; ?>
The numMOnths function appears to work correctly, using your logic, and is more efficient than my original routine.
Thanks a lot!
__________________
Jeremy Miller
Please login or register to view this content. Registration is FREE
|
|
|
|
05-13-2008, 08:38 PM
|
Re: Difference between 2 dates -- know something better?
|
Posts: 5
Name: absolut nod
|
I need to find the number of days between 2 dates.
could you please help me??
|
|
|
|
05-13-2008, 11:24 PM
|
Re: Difference between 2 dates -- know something better?
|
Posts: 88
Name: programmer
Location: internet
|
Here are some date functions I made that I use in many of my scripts:
PHP Code:
//convert unix to month day year //returns array [0]->month [1]->day [2]->year function unixToDate($unix) { $date = date('m-d-y',$unix); $date_array = explode("-", $date); return $date_array; }
//converts month day year to unix stamp //sample usage: $unix = dateToUnix(0, 0, 0, $_POST['month'], $_POST['day'], $_POST['year']); function dateToUnix($month,$day,$year) { $unix = mktime(0,0,0,ltrim($month, '0'),ltrim($day, '0'),ltrim($year, '0')); return $unix; }
//returns the number of days difference between two dates //dates must be sent to function in unix! //returns number of days ie 1 2 ... 12 13 etc... function dateDiff($earlier_date, $later_date) { $diff_in_secs=$later_date - $earlier_date; $diff_in_days=$diff_in_secs / 86400; return $diff_in_days; }
I know that doesn't help find the number of months between two dates but maybe it would be useful somehow.... I see abs0lut could use the dateDiff function
|
|
|
|
05-14-2008, 02:06 PM
|
Re: Difference between 2 dates -- know something better?
|
Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
|
OK. I sat down to redo the day calculation based off of Null's more intelligent implication and came up with this:
PHP Code:
<?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); return ceil($difference/(60*60*24)); } echo numDays('1/1/08','1/3/08').'<br />'; echo numDays('2/28/08','3/1/08').'<br />'; echo numDays('1/1/08','12/31/08').'<br />'; echo numDays('1/1/07','12/31/08').'<br />'; ?>
The idea, modified from the month one, is to start at the first second of the first day and end on the last second of the last day, so that seconds-based calculations are accurate.
__________________
Jeremy Miller
Please login or register to view this content. Registration is FREE
Last edited by JeremyMiller; 05-14-2008 at 02:06 PM..
Reason: Forgot the code tags!
|
|
|
|
05-15-2008, 01:31 AM
|
Re: Difference between 2 dates -- know something better?
|
Posts: 5
Name: absolut nod
|
Quote:
Originally Posted by Inet411
Here are some date functions I made that I use in many of my scripts:
PHP Code:
//convert unix to month day year //returns array [0]->month [1]->day [2]->year function unixToDate($unix) { $date = date('m-d-y',$unix); $date_array = explode("-", $date); return $date_array; }
//converts month day year to unix stamp //sample usage: $unix = dateToUnix(0, 0, 0, $_POST['month'], $_POST['day'], $_POST['year']); function dateToUnix($month,$day,$year) { $unix = mktime(0,0,0,ltrim($month, '0'),ltrim($day, '0'),ltrim($year, '0')); return $unix; }
//returns the number of days difference between two dates //dates must be sent to function in unix! //returns number of days ie 1 2 ... 12 13 etc... function dateDiff($earlier_date, $later_date) { $diff_in_secs=$later_date - $earlier_date; $diff_in_days=$diff_in_secs / 86400; return $diff_in_days; }
I know that doesn't help find the number of months between two dates but maybe it would be useful somehow.... I see abs0lut could use the dateDiff function
|
thank you very much
|
|
|
|
|
« Reply to Difference between 2 dates -- know something better?
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|