By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,498 Members | 1,564 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,498 IT Pros & Developers. It's quick & easy.

generating expired time between two dates?

P: n/a
LRW
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
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
de**@celticbear.com (LRW) wrote in message
news:<3a**************************@posting.google. com>...

I'm trying to get the difference between two dates, formatted in:
Time elapsed: X years, X months, X days


$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
Jul 17 '05 #2

P: n/a
de**@celticbear.com (LRW) wrote in message
news:<3a**************************@posting.google. com>...

I'm trying to get the difference between two dates, formatted in:
Time elapsed: X years, X months, X days


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
Jul 17 '05 #3

P: n/a
LRW
nc@iname.com (Nikolai Chuvakhin) wrote in message news:<32**************************@posting.google. com>...
de**@celticbear.com (LRW) wrote in message
news:<3a**************************@posting.google. com>...

I'm trying to get the difference between two dates, formatted in:
Time elapsed: X years, X months, X days


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


Excellent! Thanks!!
=)
Liam
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.