Date Math

Date Math

am 20.04.2010 17:40:39 von Floyd Resler

I need to get the difference in months between two dates. The dates =
could be as much as 60 months apart. Is there any easy way to do this =
either through PHP or MySQL? I know how I can do it through code but =
thought there might be a simple one or two line option.

Thanks!
Floyd


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

Re: Date Math

am 20.04.2010 17:45:36 von Dan Joseph

--00163630f40ffeb34e0484acf766
Content-Type: text/plain; charset=ISO-8859-1

On Tue, Apr 20, 2010 at 11:40 AM, Floyd Resler wrote:

> I need to get the difference in months between two dates. The dates could
> be as much as 60 months apart. Is there any easy way to do this either
> through PHP or MySQL? I know how I can do it through code but thought there
> might be a simple one or two line option.
>
>
check out:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functio ns.html

--
-Dan Joseph

www.canishosting.com - Unlimited Hosting Plans start @ $3.95/month. Promo
Code "NEWTHINGS" for 10% off initial order

http://www.facebook.com/canishosting
http://www.facebook.com/originalpoetry

--00163630f40ffeb34e0484acf766--

Re: Date Math

am 20.04.2010 21:32:58 von TedD

At 11:40 AM -0400 4/20/10, Floyd Resler wrote:
>I need to get the difference in months between two dates. The dates
>could be as much as 60 months apart. Is there any easy way to do
>this either through PHP or MySQL? I know how I can do it through
>code but thought there might be a simple one or two line option.
>
>Thanks!
>Floyd
>



$date1 = '2009-02-27';
$date2 = '2004-12-03';

$udate1 = strtotime($date1);
$udate2 = strtotime($date2);

$difference = $udate1 - $udate2;

$months_difference = floor($difference / 2678400);

echo("The difference is $months_difference months");

?>

I this will work, but the question of what constitutes a month might
come into play.

Cheers,

tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com

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

Re: Date Math

am 20.04.2010 22:17:16 von Paul M Foster

On Tue, Apr 20, 2010 at 03:32:58PM -0400, tedd wrote:

> At 11:40 AM -0400 4/20/10, Floyd Resler wrote:
>> I need to get the difference in months between two dates. The dates
>> could be as much as 60 months apart. Is there any easy way to do
>> this either through PHP or MySQL? I know how I can do it through
>> code but thought there might be a simple one or two line option.
>>
>> Thanks!
>> Floyd
>>
>
>
> >
> $date1 = '2009-02-27';
> $date2 = '2004-12-03';
>
> $udate1 = strtotime($date1);
> $udate2 = strtotime($date2);

$from = getdate($udate1);
$from_year = $from['year'];
$from_month = $from['month'];

$to = getdate($udate2);
$to_year = $to['year'];
$to_month = $to['month'];

// Assumes $to_date is later than $from_date

if ($from_year == $to_year)
$months = $to_month - $from_month;
elseif ($to_month >= $from_month) {
$num_years = $to_year - $from_year;
$add_months = $num_years * 12;
$base_months = $to_month - $from_month;
$months = $base_months + $add_months;
}
else { // $to_month < $from_month
$num_years = $to_year - $from_year;
$base_months = $num_years * 12;
$sub_months = $from_month - $to_month;
$months = $base_months - $sub_months;
}

This will give the months between two dates, ignoring the actual day of
the month for each date (may not be exactly what you want, and code is
untested).

>
> $difference = $udate1 - $udate2;
>
> $months_difference = floor($difference / 2678400);
>
> echo("The difference is $months_difference months");
>
> ?>
>
> I this will work, but the question of what constitutes a month might
> come into play.

My code above is submitted because I don't like doing calculations with
seconds. Tedd's right, the OP's questions can't be answered precisely
because months vary in number of days.

Paul

--
Paul M. Foster

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

Re: Date Math

am 21.04.2010 11:39:56 von Michiel Sikma

--0016364267df19703d0484bbfad2
Content-Type: text/plain; charset=UTF-8

On 20 April 2010 17:40, Floyd Resler wrote:

> I need to get the difference in months between two dates. The dates could
> be as much as 60 months apart. Is there any easy way to do this either
> through PHP or MySQL? I know how I can do it through code but thought there
> might be a simple one or two line option.
>
> Thanks!
> Floyd
>
>
You're best off doing this in MySQL.
Something like select timestampdiff(month, '2010-01-01', '2010-05-22');
should work.

Michiel

--0016364267df19703d0484bbfad2--

Re: Date Math

am 21.04.2010 14:37:00 von Floyd Resler

On Apr 21, 2010, at 5:39 AM, Michiel Sikma wrote:

> On 20 April 2010 17:40, Floyd Resler wrote:
>=20
>> I need to get the difference in months between two dates. The dates =
could
>> be as much as 60 months apart. Is there any easy way to do this =
either
>> through PHP or MySQL? I know how I can do it through code but =
thought there
>> might be a simple one or two line option.
>>=20
>> Thanks!
>> Floyd
>>=20
>>=20
> You're best off doing this in MySQL.
> Something like select timestampdiff(month, '2010-01-01', =
'2010-05-22');
> should work.
>=20
> Michiel

Perfect! That's exactly what I was looking for!=

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