# calculate time difference in mintues

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

•  Subject
• Author
• Posted on
Hello,

I have 2 time fields dynamically generated in format "m/d/y H:m".
Could someone show me a good function to calculate the time interval
difference in minutes? I played with strtotime() but but that only
gave me difference in hours and only if the times were on the same day
(after wrapping with date() function). TIA

## Re: calculate time difference in mintues

function dateDiff(\$firstDate, \$secondDate, \$interval = 'd')
{
\$swapDate   = \$firstDate;
\$firstDate  = min(\$swapDate, \$secondDate);
\$secondDate = max(\$swapDate, \$secondDate);
if (\$interval == 'm' || \$interval == 'y')
{
\$firstYear    = date('Y', \$firstDate);
\$secondYear   = date('Y', \$secondDate);
\$year         = \$secondYear - \$firstYear;
if (\$firstYear != \$secondYear){ \$year--; }
if (\$interval == 'y'){ return \$year; }
\$firstMonth   = date('n', \$firstDate);
\$secondMonth  = date('n', \$secondDate);
\$month        = \$firstYear == \$secondYear       ?
abs(\$firstMonth - \$secondMonth) :
12 - \$firstMonth + \$secondMonth ;
return \$month % 12 + (12 * \$year);
}
\$intervals = array(
'w' => (7  * 24 * 60 * 60)  ,
'd' => (24 * 60 * 60)       ,
'h' => (60 * 60)            ,
'n' => 60                   ,
's' => 1
);
return floor(abs(\$firstDate - \$secondDate) / \$intervals[\$interval]);
}

## Re: calculate time difference in mintues

Jessica Griego wrote:

Which provides incorrect results if it happens to cross an odd number of
daylight savings time changes.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: calculate time difference in mintues

Only if daylight savings time defined for the locale. A simple adjustment
can be made to offset the difference. No big deal.

## Re: calculate time difference in mintues

Jessica Griego wrote:

Which does not mean your code is correct.  It is not.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: calculate time difference in mintues

Well first, Mr. Stuckle, I didn't know this was a competition. Second, you
offer nothing but complaints. At least have the courtesy to 'compete' before
complaining. Third, mine is absolutely correct outside of DST..even so, as
I've already mentioned, that is easily handled. Again, no big deal.

Hat's off to Sheldon. Well done. As for you? Well, 'put up' is about the
only sentiment I can share with you at this point. If I'm mistaking your
tone in this posting as childishly arrogant when in fact you are not, I'm
truly sorry. From what I've just read of your dealings with others in this
newsgroup, my intuition seems accurate.

Have a nice day Mr. Stuckle.

## Re: calculate time difference in mintues

Now that I've looked at this, Jerry, please tell me the difference between
this:

(strtotime(\$date2) - strtotime(\$date1) + 30) / 60
// Notice that Sheldon didn't test his suggestion...
// which would have blown up because of the extra '('
// in it that I removed above.
// You apparently did not test it either.

And this:

abs(\$firstDate - \$secondDate) / \$intervals[\$interval])
// Where the date variables above are already time...
// and are being divided by 60 ('n' for minutes for the interval).

Seems that not only is mine correct, it matches Sheldon's identically -
minus his 30 second rounding - but that I actually posted mine first AND
tested it. Your support for Sheldon's code was that it handled DST. It does
not. I expect to see your DST enabled version now. That, or a post saying
"I, Jerry Stuckle, am pompous and brash."

BTW, I know what Sheldon is going for, however I don't think you do nor can
you make a simple correction to his code that will make it DST enabled.
We'll see.

:^)

## Re: calculate time difference in mintues

Jessica Griego wrote:

ROFLMAO.  A minor syntax error does not make the idea invalid - unlike

And obviously you did NOT check his code versus your across a
standard/daylight savings time change - or you would have found the
difference.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: calculate time difference in mintues

LOL. So, you are relenting that you have 1) not tested Sheldon's code with
the odd DST time period scenario, and that 2) you cannot find a difference
between:

(strtotime(\$date2) - strtotime(\$date1) + 30) / 60

and

abs(\$firstDate - \$secondDate) / \$intervals[\$interval])

and further, that the former mysteriously handles DST while the latter
doesn't. RMAOL...there is no difference and what's more, I have tested it.
brrrrrr, thank you for playing though, Jerry!

Again, Jerry, what simple adjustment has to happen to Sheldon's code in
order for it to be DST enabled? Come on...show us what a big boy
professional programmer you are. :)

## Re: calculate time difference in mintues

Jessica Griego wrote:

ROFLMAO!  And you said you had plonked me!  So now you're a liar, also.

This is a REAL <*PLONK*>

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: calculate time difference in mintues

Well, I thought I'd give you a chance to defend your apparent stupidity,
Jerry. The two equations are the same and neither support DST. Plus, you
don't know how to correct them so they are DST aware. I can see now that you
really DONT know anything other than you've made yourself look foolish. That
is evident by your red-herring present here.

And the world is better for you shutting yourself up. :)

## Re: calculate time difference in mintues

Jerry Stuckle wrote:

I had to look back myself, I was sure there was a PLONK in there somewhere.

If you going to PLONK someone then do it. Sheesh.

## Re: calculate time difference in mintues

student4lifer@gmail.com wrote:

Wouldn't this work

\$timediff = (strtotime((\$date2) - strtotime(\$date1) + 30) / 60

assuming you want rounding to the nearest minute?

## Re: calculate time difference in mintues

After this interesting discussion...

Jessica's solution needs its input in seconds, so it requires you to call
strtotime() yourself before calling that routine. It will then work,
including those cases where DST is an odd number of times. It has the
advantage that you can apply it also in cases where you want the difference
in hours, months or even years.
This said, Sheldon's solution does what you ask, and nothing more.

Please be aware that "m/d/y H:m" is not very well formed. I for instance
would read "07/06/05 12:00" as year 7, month 6, day 5, noon.  Someone else
(you) may read it as month 7, day 6, year 5. Still someone else may think it
means day 7, month 6, year 5. And some poor soul may think it's midnight,
which I would write as 00:00.

\$ php -f test.php ; unixtime 1183802400;unixtime 1194606000
timediff is set to 180060
Unix timestamp 1: 1183802400
Unix timestamp 2: 1194606000
125 days, 1 hours, 0 minutes and 0 seconds
which is 180060 minutes
1st result of dateDiff: 0
2nd result of dateDiff: 180060
2007-07-07T10:00:00Z
2007-11-09T11:00:00Z

All solutions return 180060 which is, indeed, correct.

bash function unixtime:

unixtime ()
{
/bin/date -ud 1970-01-01\ 00:00\ +0000\ +sec +%Y-%m-%dT%H:%M:%SZ
}

contents of test.php:

<?php

/* This code is NOT intended to show off any programming skills. I know much
* of it can be left out, taken together, and so on.
*/

\$date1="07/07/07 12:00"; // warning: USA-centric mm/dd/yy , not ISO yy/mm/dd
\$date2="11/09/07 12:00";

// modified sheldong example
\$timediff = floor((strtotime(\$date2) - strtotime(\$date1) + 30) / 60);

echo "timediff is set to \$timediff\n";

// that example worked out a bit more
\$unixtime1=strtotime(\$date1);
\$unixtime2=strtotime(\$date2);

echo "Unix timestamp 1: \$unixtime1\n";
echo "Unix timestamp 2: \$unixtime2\n";

\$diff=abs(\$unixtime1-\$unixtime2);

\$days=floor(\$diff/86400);
\$rest=\$diff-\$days*86400;
\$hours=floor(\$rest/3600);
\$rest=\$rest-\$hours*3600;
\$minutes=floor(\$rest/60);
\$rest=\$rest-\$minutes*60;

\$totalminutes=\$days*1440+\$hours*60+\$minutes;
if (\$rest>29) \$totalminutes++;

echo "\$days days, \$hours hours, \$minutes minutes and \$rest seconds\n";
echo "which is \$totalminutes minutes\n";

function dateDiff(\$firstDate, \$secondDate, \$interval = 'd')
{
\$swapDate   = \$firstDate;
\$firstDate  = min(\$swapDate, \$secondDate);
\$secondDate = max(\$swapDate, \$secondDate);
if (\$interval == 'm' || \$interval == 'y')
{
\$firstYear    = date('Y', \$firstDate);
\$secondYear   = date('Y', \$secondDate);
\$year         = \$secondYear - \$firstYear;
if (\$firstYear != \$secondYear){ \$year--; }
if (\$interval == 'y'){ return \$year; }
\$firstMonth   = date('n', \$firstDate);
\$secondMonth  = date('n', \$secondDate);
\$month        = \$firstYear == \$secondYear       ?
abs(\$firstMonth - \$secondMonth) :
12 - \$firstMonth + \$secondMonth ;
return \$month % 12 + (12 * \$year);
}
\$intervals = array(
'w' => (7  * 24 * 60 * 60)  ,
'd' => (24 * 60 * 60)       ,
'h' => (60 * 60)            ,
'n' => 60                   ,
's' => 1
);
return floor(abs(\$firstDate - \$secondDate) / \$intervals[\$interval]);
}

\$result=dateDiff(\$date1,\$date2,"n");
echo "1st result of dateDiff: \$result\n";

// modified dateDiff example, because it expects time as its inputs, not
"mm/dd/yy H:m"

\$result=dateDiff(\$unixtime1,\$unixtime2,"n");
echo "2nd result of dateDiff: \$result\n";

?>

You could also specify a time zone to the dates. It even works for UTC:

Modification in test.php:
\$date1="07/07/07 12:00 UTC"; // warning: USA-centric mm/dd/yy , not ISO
yy/mm/dd
\$date2="11/09/07 12:00 UTC";

run it:
\$ \$ php -f test.php ; unixtime 1183809600;unixtime 1194609600
timediff is set to 180000
Unix timestamp 1: 1183809600
Unix timestamp 2: 1194609600
125 days, 0 hours, 0 minutes and 0 seconds
which is 180000 minutes
1st result of dateDiff: 0
2nd result of dateDiff: 180000
2007-07-07T12:00:00Z
2007-11-09T12:00:00Z

As you can see: all solutions no longer have that extra hour.