tags:

views:

32

answers:

1
$dateDiff    = $mtime - $ctime;
$fullDays    = floor($dateDiff/(60*60*24));
$fullHours   = floor(($dateDiff-($fullDays*60*60*24))/(60*60));
$fullMinutes = floor(($dateDiff-($fullDays*60*60*24)-($fullHours*60*60))/60);
echo "Difference is $fullDays days, $fullHours hours and $fullMinutes minutes."; 

I am looking to add the ability to see weeks as well. I know I could just repeat the same process, but is there a library or an easier way?

+1  A: 

In PHP 5.3 and up, there's the DateTime class. See http://php.net/manual/en/class.datetime.php.

MvanGeest
I was about to say the same thing, but the DateInterval class which you get when diff'ing two DateTimes really doesn't make things all that much easier, because you'll still need to multiply the amount of days by 24, and hours by 60. And weeks? It doesn't offer a Week option... so you're back at square one.Technically, it's probably still better to use for some OOP-reasons. But not easier, I'm afraid.
kander
[DateInterval::format](http://us2.php.net/manual/en/dateinterval.format.php) *does* include options to show months and days, as well as hours, minutes and seconds. You're right @kander, it doesn't do weeks... but the OP didn't ask for weeks. You can get the total number of days, though, and calculate weeks from that easily.
Charles
Charles, I never said it doesn't offer those options. What I did say is that to get the result the topic starter asked for, that is, the total amount of minutes, you'll still be multiplying. If I get the TimeInterval between Now and 1 hour 20 minutes ago, I don't get 80 minutes. I get 20 minutes, and I get 1 hour. So my point was that since you'll "still need to multiply the amount of days by 24, and hours by 60" in order to arrive at the answer being asked, using DateTime isn't much easier. I guess I should have phrased that more clearly, obviously. Sorry for the misunderstanding caused!
kander