# generating expired time between two dates?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
I'm trying to get the difference between two dates, formatted in:
Time elapsed: X years, X months, X days
but I can't seem to get it right.

Here's my last attempt:

<?php
\$oldtime = "2004-01-05";
\$oldtime = explode("-", \$oldtime);
\$otY = \$oldtime[0];
\$otM = \$oldtime[1];
\$otD = \$oldtime[2];

\$time = date('Y-m-d');
\$time = explode("-", \$time);
\$tY = \$time[0];
\$tM = \$time[1];
\$tD = \$time[2];

\$difY = (mktime(0, 0, 0, 0, 0, \$tY)) - (mktime(0, 0, 0, 0, 0, \$otY));
\$difM = (mktime(0, 0, 0, \$tM, 0, 0)) - (mktime(0, 0, 0, \$otM, 0, 0));
\$difD = (mktime(0, 0, 0, 0, \$tD, 0)) - (mktime(0, 0, 0, 0, \$otD, 0));

echo "time: ".\$difY." years,".\$difM." months,".\$difD." days";
?>

What that gets is:
time: 0 years,18399600 months,1900800 days

That "0 years" seems positive, since that's the correct result, but I
can't figure out what's going on with the months and days.

I'm tired so I'm probably just missing it, but any hint at what I need
to look at, I'd really appreciate!
Thanks,
Liam

## Re: generating expired time between two dates?

deja@celticbear.com (LRW) wrote in message

\$oldtime = strtotime("2004-01-05");
\$time = time();
\$elapsed = \$time - \$oldtime;
\$years = date('Y') - 1970;
\$months = date('n');
\$days = date('j');
echo "Time elapsed: \$years years, \$months months, \$days days";

Cheers,
NC

## Re: generating expired time between two dates?

deja@celticbear.com (LRW) wrote in message

Sorry, the previous post was sent out incomplete.
The correct code is as follows:

\$oldtime = strtotime("2004-01-05");
\$time = time();
\$elapsed = \$time - \$oldtime;
\$years = date('Y', \$elapsed) - 1970;
\$months = date('n', \$elapsed);
\$days = date('j', \$elapsed);
echo "Time elapsed: \$years years, \$months months, \$days days";

Cheers,
NC

## Re: generating expired time between two dates?

nc@iname.com (Nikolai Chuvakhin) wrote in message

Excellent! Thanks!!
=)
Liam