Re: Months between two dates

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I will assume that your dates are stored in two variables $date1 and $date2

<code>
$date1array = explode("/",$date1);
$date2array = explode("/",$date2);

// Assume Date 2 is later than Date 1

$months_apart = ($date2array[2] - $date1array[2])*12;

// Month is later in date 1 than in date 2
// Find the difference of months
if( $date1array[0] > $date2array[0] )
{
	$months_apart -= $date1array[0] - $date2array[0];
}
else
{
	$months_apart += $date2array[0] - $date1array[0];
}

// $months_apart should now contain months apart
</code>

And there is your quick and dirty method,
I haven't really debugged it, but I am pretty sure that should work.
Some (int) casting my be necessary, but I think PHP will automatically
do that for you.

Good luck,
Kyle

Phillip Baker wrote:
> Greetings Gents,
> 
> I want to take two dates formatted as
> 11/1/1998
> 10/1/2008
> 
> And find out how many months have elapsed between them.
> I was wondering if there was a quick and dirty way to do this without a for
> loop.
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux