473,487 Members | 2,461 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

calculate time difference in mintues

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
Nov 9 '08 #1
15 6393
<st***********@gmail.comwrote in message
news:e3**********************************@v39g2000 pro.googlegroups.com...
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
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]);
}
Nov 9 '08 #2
On Nov 9, 12:56*am, "Jessica Griego" <j...@example.comwrote:
<student4li...@gmail.comwrote in message

news:e3**********************************@v39g2000 pro.googlegroups.com...
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

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]);

}- Hide quoted text -

- Show quoted text -
Thanks...found what I wanted also from date_parse() and
gregoriantojd().
Nov 9 '08 #3
st***********@gmail.com wrote:
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

Wouldn't this work

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

assuming you want rounding to the nearest minute?
Nov 9 '08 #4
Jessica Griego wrote:
<st***********@gmail.comwrote in message
news:e3**********************************@v39g2000 pro.googlegroups.com...
>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

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]);
}

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.
js*******@attglobal.net
==================
Nov 9 '08 #5

"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:gf**********@registered.motzarella.org...
Jessica Griego wrote:
><st***********@gmail.comwrote in message
news:e3**********************************@v39g200 0pro.googlegroups.com...
>>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

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]);
}


Which provides incorrect results if it happens to cross an odd number of
daylight savings time changes.
Only if daylight savings time defined for the locale. A simple adjustment
can be made to offset the difference. No big deal.
Nov 9 '08 #6
Jessica Griego wrote:
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:gf**********@registered.motzarella.org...
>Jessica Griego wrote:
>><st***********@gmail.comwrote in message
news:e3**********************************@v39g20 00pro.googlegroups.com...
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
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]);
}

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

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

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

Sheldon's answer is much better. No adjustment required.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Nov 9 '08 #7

"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:gf**********@registered.motzarella.org...
Jessica Griego wrote:
>"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:gf**********@registered.motzarella.org...
>>Jessica Griego wrote:
<st***********@gmail.comwrote in message
news:e3**********************************@v39g2 000pro.googlegroups.com...
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
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]);
}
Which provides incorrect results if it happens to cross an odd number of
daylight savings time changes.

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

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

Sheldon's answer is much better. No adjustment required.
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.
Nov 10 '08 #8
Jessica Griego wrote:
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:gf**********@registered.motzarella.org...
>Jessica Griego wrote:
>>"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:gf**********@registered.motzarella.org...
Jessica Griego wrote:
<st***********@gmail.comwrote in message
news:e3**********************************@v39g 2000pro.googlegroups.com...
>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
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]);
}
>
>
Which provides incorrect results if it happens to cross an odd number of
daylight savings time changes.
Only if daylight savings time defined for the locale. A simple adjustment
can be made to offset the difference. No big deal.
Which does not mean your code is correct. It is not.

Sheldon's answer is much better. No adjustment required.

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.
This is not a competition. But if the answer is incorrect, it deserves
to be indicated as such.

I would have had the same answer as Sheldon, but he beat me to it. And
just because yours is "absolutely correct outside of DST" does not make
it correct. It's the type of programming (and thinking) which leads to
bugs which can be very hard to find.
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.

Quite frankly, I really don't give a damn if you "put up" with me or
not. There was a problem with you code. I *nicely* called attention to
it. Rather than just admit the problem - and bug - you get all upset
because someone point out a bug in your code. HOW DARE SOMEONE DO THAT!

Get over it.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Nov 10 '08 #9

"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:gf**********@registered.motzarella.org...
Jessica Griego wrote:
>"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:gf**********@registered.motzarella.org...
>>Jessica Griego wrote:
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:gf**********@registered.motzarella.org.. .
Jessica Griego wrote:
><st***********@gmail.comwrote in message
>news:e3**********************************@v39 g2000pro.googlegroups.com...
>>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
>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]);
>}
>>
>>
Which provides incorrect results if it happens to cross an odd number
of daylight savings time changes.
Only if daylight savings time defined for the locale. A simple
adjustment can be made to offset the difference. No big deal.
Which does not mean your code is correct. It is not.

Sheldon's answer is much better. No adjustment required.

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.

This is not a competition. But if the answer is incorrect, it deserves to
be indicated as such.
The answer is correct. It is also an isolated function. That means things
like localization functions that specifically deal with DST, what the first
day of the week is, etc. handle things like offsetting the results to match
what the locale answer should be. As it is, one can easily put an argument
in the function the flag whether or not DST is in effect...from there, the
math is so glaringly obvious it's not even funny.

What I'd expect in 'correction', Jerry, is not just 'Hey, that's wrong'. I'd
expect to see an alternative that does exactly what the OP stated without
whatever problems you see.
I would have had the same answer as Sheldon, but he beat me to it. And
just because yours is "absolutely correct outside of DST" does not make it
correct. It's the type of programming (and thinking) which leads to bugs
which can be very hard to find.
You're not even making sense here! It is *absolutely* correct 100% of the
time where I live...Arizona! The results make it correct...so, yes, it is
correct. Again, if you live somewhere else and find that an adjustment needs
to be made to handle DST, then make it.

As for Sheldon beating you to it...suuuure, that's the ticket!

<snip>
Quite frankly, I really don't give a damn if you "put up" with me or not.
There was a problem with you code. I *nicely* called attention to it.
Rather than just admit the problem - and bug - you get all upset because
someone point out a bug in your code. HOW DARE SOMEONE DO THAT!
You seem illiterate, Jerry. "Put up" is the predicate to "Shut up". We are
all used to putting up with trolls on UseNet. I was saying that you should
be able to post a correction rather than just being able to use the word
'incorrect'. As for *nicely*, I nicely pointed out that my code is not
incorrect and that if someone wants to account for DST because it applies to
them, they can and easily. I've said it was no big deal. You keep steering
this into an argument. Just drop it.

From your other posts in this forum, I too get the feeling you are just a
troll. Either drop it, or don't. I'd suggest the former as the latter makes
you look childish.

EOT
Nov 10 '08 #10
Jessica Griego wrote:
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:gf**********@registered.motzarella.org...
>Jessica Griego wrote:
>>"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:gf**********@registered.motzarella.org...
Jessica Griego wrote:
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:gf**********@registered.motzarella.org. ..
>Jessica Griego wrote:
>><st***********@gmail.comwrote in message
>>news:e3**********************************@v3 9g2000pro.googlegroups.com...
>>>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
>>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]);
>>}
>>>
>>>
>Which provides incorrect results if it happens to cross an odd number
>of daylight savings time changes.
Only if daylight savings time defined for the locale. A simple
adjustment can be made to offset the difference. No big deal.
Which does not mean your code is correct. It is not.

Sheldon's answer is much better. No adjustment required.
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.
This is not a competition. But if the answer is incorrect, it deserves to
be indicated as such.

The answer is correct. It is also an isolated function. That means things
like localization functions that specifically deal with DST, what the first
day of the week is, etc. handle things like offsetting the results to match
what the locale answer should be. As it is, one can easily put an argument
in the function the flag whether or not DST is in effect...from there, the
math is so glaringly obvious it's not even funny.

What I'd expect in 'correction', Jerry, is not just 'Hey, that's wrong'. I'd
expect to see an alternative that does exactly what the OP stated without
whatever problems you see.
Sorry, the answer is not correct - for the exact reason I outlined. It
does not work when the daylight savings time crosses an odd number of
DST changes.

You did not point out how to handle offsetting the results, nor did your
code contain any way to handle this occurrence.

And I did point out a valid answer - the one from Sheldon, which is the
same one I would have posted. And it does not suffer from the DST problem.

>I would have had the same answer as Sheldon, but he beat me to it. And
just because yours is "absolutely correct outside of DST" does not make it
correct. It's the type of programming (and thinking) which leads to bugs
which can be very hard to find.

You're not even making sense here! It is *absolutely* correct 100% of the
time where I live...Arizona! The results make it correct...so, yes, it is
correct. Again, if you live somewhere else and find that an adjustment needs
to be made to handle DST, then make it.
That's fine - in Arizona you do not (currently) observe DST. That does
not make it correct for the rest of the world!
As for Sheldon beating you to it...suuuure, that's the ticket!
Yep. I'm not on here 24/7. But if I would have seen that post before
Sheldon did, I would have presented roughly the same solution.
<snip>
>Quite frankly, I really don't give a damn if you "put up" with me or not.
There was a problem with you code. I *nicely* called attention to it.
Rather than just admit the problem - and bug - you get all upset because
someone point out a bug in your code. HOW DARE SOMEONE DO THAT!

You seem illiterate, Jerry. "Put up" is the predicate to "Shut up". We are
all used to putting up with trolls on UseNet. I was saying that you should
be able to post a correction rather than just being able to use the word
'incorrect'. As for *nicely*, I nicely pointed out that my code is not
incorrect and that if someone wants to account for DST because it applies to
them, they can and easily. I've said it was no big deal. You keep steering
this into an argument. Just drop it.
You don't seem to understand plain English. But then most trolls don't.
And there's only one troll here - YOU.

I did post a correction. I pointed out what was wrong with your code
and pointed to Sheldon's solution as the correct one. I saw no reason
to post the same code a second time.
From your other posts in this forum, I too get the feeling you are just a
troll. Either drop it, or don't. I'd suggest the former as the latter makes
you look childish.

EOT

You're the only one acting childish here - insisting your code is
correct when it isn't - for the very solid reason I gave.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Nov 10 '08 #11

"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:gf**********@registered.motzarella.org...
Jessica Griego wrote:
>"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:gf**********@registered.motzarella.org...
>>Jessica Griego wrote:
<st***********@gmail.comwrote in message
news:e3**********************************@v39g2 000pro.googlegroups.com...
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
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]);
}
Which provides incorrect results if it happens to cross an odd number of
daylight savings time changes.

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

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

Sheldon's answer is much better. No adjustment required.
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.

:^)
Nov 10 '08 #12

"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:gf**********@registered.motzarella.org...
Jessica Griego wrote:
>"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:gf**********@registered.motzarella.org...
>>Jessica Griego wrote:
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:gf**********@registered.motzarella.org.. .
Jessica Griego wrote:
>"Jerry Stuckle" <js*******@attglobal.netwrote in message
>news:gf**********@registered.motzarella.org.. .
>>Jessica Griego wrote:
>>><st***********@gmail.comwrote in message
>>>news:e3**********************************@v 39g2000pro.googlegroups.com...
>>>>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
>>>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]);
>>>}
>>>>
>>>>
>>Which provides incorrect results if it happens to cross an odd
>>number of daylight savings time changes.
>Only if daylight savings time defined for the locale. A simple
>adjustment can be made to offset the difference. No big deal.
Which does not mean your code is correct. It is not.
>
Sheldon's answer is much better. No adjustment required.
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.

This is not a competition. But if the answer is incorrect, it deserves
to be indicated as such.

The answer is correct. It is also an isolated function. That means things
like localization functions that specifically deal with DST, what the
first day of the week is, etc. handle things like offsetting the results
to match what the locale answer should be. As it is, one can easily put
an argument in the function the flag whether or not DST is in
effect...from there, the math is so glaringly obvious it's not even
funny.

What I'd expect in 'correction', Jerry, is not just 'Hey, that's wrong'.
I'd expect to see an alternative that does exactly what the OP stated
without whatever problems you see.

Sorry, the answer is not correct - for the exact reason I outlined. It
does not work when the daylight savings time crosses an odd number of DST
changes.
Sorry, in Arizona we don't use DST...my results are ALWAYS correct. If you
use DST, adjust for it yourself.
You did not point out how to handle offsetting the results, nor did your
code contain any way to handle this occurrence.
Because in the code lib in which I have it, I have a localization class that
adjusts not only for DST, but also for the beginning of the week - for when
this function is asked to return the difference of two dates in weeks...you
weren't smart enough to catch that either!

This function does exactly what I want it to do and returns consistently
correct results. As it is, Jerry, you can't seem to even muster a '$' sign
of code. Further, if you are going to argue the fact that I don't show *how*
to adjust for DST, it means you probably don't have a CLUE as to how to do
it! That's funny to me, since it's about as easy as 2 and 2. That's ok, I'll
mute my troll meter at this point since that seems it's all you're doing.
And I did point out a valid answer - the one from Sheldon, which is the
same one I would have posted. And it does not suffer from the DST
problem.
Actually, not only will it cause php to explode as it is written, it will
NOT handle DST...as written. Had you, or he, actually tested it you'd know
that it doesn't. Further, you don't know what to do to correct Sheldon's
oversight so that it does work...but I'm sure Sheldon does! You make me
laugh. :^)
>>I would have had the same answer as Sheldon, but he beat me to it. And
just because yours is "absolutely correct outside of DST" does not make
it correct. It's the type of programming (and thinking) which leads to
bugs which can be very hard to find.

You're not even making sense here! It is *absolutely* correct 100% of the
time where I live...Arizona! The results make it correct...so, yes, it is
correct. Again, if you live somewhere else and find that an adjustment
needs to be made to handle DST, then make it.

That's fine - in Arizona you do not (currently) observe DST. That does
not make it correct for the rest of the world!
It make it correct THE WORLD OVER when NOT IN DST !!! It also makes it VERY
easy to offset when the WORLD IS IN DST. I have a localization class that
works all of that out very nicely.
>As for Sheldon beating you to it...suuuure, that's the ticket!

Yep. I'm not on here 24/7. But if I would have seen that post before
Sheldon did, I would have presented roughly the same solution.
From what I can tell, you're here 23.75/7 annoying the hell out of most of
UseNet.
><snip>
>>Quite frankly, I really don't give a damn if you "put up" with me or
not. There was a problem with you code. I *nicely* called attention to
it. Rather than just admit the problem - and bug - you get all upset
because someone point out a bug in your code. HOW DARE SOMEONE DO THAT!

You seem illiterate, Jerry. "Put up" is the predicate to "Shut up". We
are all used to putting up with trolls on UseNet. I was saying that you
should be able to post a correction rather than just being able to use
the word 'incorrect'. As for *nicely*, I nicely pointed out that my code
is not incorrect and that if someone wants to account for DST because it
applies to them, they can and easily. I've said it was no big deal. You
keep steering this into an argument. Just drop it.

You don't seem to understand plain English. But then most trolls don't.
And there's only one troll here - YOU.
Lol. So, your lack of rebuttal is to make a strong case for your literacy?
"I'm rubber and you're glue"...that's to make you appear sophisticated and
NOT a troll.
I did post a correction. I pointed out what was wrong with your code and
pointed to Sheldon's solution as the correct one. I saw no reason to post
the same code a second time.
>From your other posts in this forum, I too get the feeling you are just a
troll. Either drop it, or don't. I'd suggest the former as the latter
makes you look childish.

EOT

You're the only one acting childish here - insisting your code is correct
when it isn't - for the very solid reason I gave.
The code is correct. And as written, Sheldon's is not...and you don't even
know why.

You obviously think as a child. It also effects your behavior and not just
your logical processes.

**PLONK**

No more trolling for you. :)
Nov 10 '08 #13
Jessica Griego wrote:
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:gf**********@registered.motzarella.org...
>Jessica Griego wrote:
>>"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:gf**********@registered.motzarella.org...
Jessica Griego wrote:
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:gf**********@registered.motzarella.org. ..
>Jessica Griego wrote:
>>"Jerry Stuckle" <js*******@attglobal.netwrote in message
>>news:gf**********@registered.motzarella.org. ..
>>>Jessica Griego wrote:
>>>><st***********@gmail.comwrote in message
>>>>news:e3**********************************@ v39g2000pro.googlegroups.com...
>>>>>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
>>>>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]);
>>>>}
>>>>>
>>>>>
>>>Which provides incorrect results if it happens to cross an odd
>>>number of daylight savings time changes.
>>Only if daylight savings time defined for the locale. A simple
>>adjustment can be made to offset the difference. No big deal.
>Which does not mean your code is correct. It is not.
>>
>Sheldon's answer is much better. No adjustment required.
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.
>
This is not a competition. But if the answer is incorrect, it deserves
to be indicated as such.
The answer is correct. It is also an isolated function. That means things
like localization functions that specifically deal with DST, what the
first day of the week is, etc. handle things like offsetting the results
to match what the locale answer should be. As it is, one can easily put
an argument in the function the flag whether or not DST is in
effect...from there, the math is so glaringly obvious it's not even
funny.

What I'd expect in 'correction', Jerry, is not just 'Hey, that's wrong'.
I'd expect to see an alternative that does exactly what the OP stated
without whatever problems you see.
Sorry, the answer is not correct - for the exact reason I outlined. It
does not work when the daylight savings time crosses an odd number of DST
changes.

Sorry, in Arizona we don't use DST...my results are ALWAYS correct. If you
use DST, adjust for it yourself.
YOU presented an incorrect solution. Period.
>You did not point out how to handle offsetting the results, nor did your
code contain any way to handle this occurrence.

Because in the code lib in which I have it, I have a localization class that
adjusts not only for DST, but also for the beginning of the week - for when
this function is asked to return the difference of two dates in weeks...you
weren't smart enough to catch that either!
Then that's the code you should have posted.
This function does exactly what I want it to do and returns consistently
correct results. As it is, Jerry, you can't seem to even muster a '$' sign
of code. Further, if you are going to argue the fact that I don't show *how*
to adjust for DST, it means you probably don't have a CLUE as to how to do
it! That's funny to me, since it's about as easy as 2 and 2. That's ok, I'll
mute my troll meter at this point since that seems it's all you're doing.
No, it does not return "consistently correct results" when the time
period spans an odd number of standard/daylight savings time changes.

It's just this type of thinking which produces hard to find errors in code.
>And I did point out a valid answer - the one from Sheldon, which is the
same one I would have posted. And it does not suffer from the DST
problem.

Actually, not only will it cause php to explode as it is written, it will
NOT handle DST...as written. Had you, or he, actually tested it you'd know
that it doesn't. Further, you don't know what to do to correct Sheldon's
oversight so that it does work...but I'm sure Sheldon does! You make me
laugh. :^)
No, it will not "cause php to explode". And it will handle DST, as written.

But we already know what kind of programmer you are.
>>>I would have had the same answer as Sheldon, but he beat me to it. And
just because yours is "absolutely correct outside of DST" does not make
it correct. It's the type of programming (and thinking) which leads to
bugs which can be very hard to find.
You're not even making sense here! It is *absolutely* correct 100% of the
time where I live...Arizona! The results make it correct...so, yes, it is
correct. Again, if you live somewhere else and find that an adjustment
needs to be made to handle DST, then make it.
That's fine - in Arizona you do not (currently) observe DST. That does
not make it correct for the rest of the world!

It make it correct THE WORLD OVER when NOT IN DST !!! It also makes it VERY
easy to offset when the WORLD IS IN DST. I have a localization class that
works all of that out very nicely.
So? There is a significant part of the world WHICH USES DST. And your
code, AS WRITTEN, does not handle that.
>>As for Sheldon beating you to it...suuuure, that's the ticket!
Yep. I'm not on here 24/7. But if I would have seen that post before
Sheldon did, I would have presented roughly the same solution.

From what I can tell, you're here 23.75/7 annoying the hell out of most of
UseNet.
ROFLMAO! Because I call attention to the sloppy code trolls like you
write, and indicate WHY it is wrong?
>><snip>

Quite frankly, I really don't give a damn if you "put up" with me or
not. There was a problem with you code. I *nicely* called attention to
it. Rather than just admit the problem - and bug - you get all upset
because someone point out a bug in your code. HOW DARE SOMEONE DO THAT!
You seem illiterate, Jerry. "Put up" is the predicate to "Shut up". We
are all used to putting up with trolls on UseNet. I was saying that you
should be able to post a correction rather than just being able to use
the word 'incorrect'. As for *nicely*, I nicely pointed out that my code
is not incorrect and that if someone wants to account for DST because it
applies to them, they can and easily. I've said it was no big deal. You
keep steering this into an argument. Just drop it.
You don't seem to understand plain English. But then most trolls don't.
And there's only one troll here - YOU.

Lol. So, your lack of rebuttal is to make a strong case for your literacy?
"I'm rubber and you're glue"...that's to make you appear sophisticated and
NOT a troll.
ROFLMAO!
>I did post a correction. I pointed out what was wrong with your code and
pointed to Sheldon's solution as the correct one. I saw no reason to post
the same code a second time.
>>From your other posts in this forum, I too get the feeling you are just a
troll. Either drop it, or don't. I'd suggest the former as the latter
makes you look childish.

EOT
You're the only one acting childish here - insisting your code is correct
when it isn't - for the very solid reason I gave.

The code is correct. And as written, Sheldon's is not...and you don't even
know why.

You obviously think as a child. It also effects your behavior and not just
your logical processes.

**PLONK**

No more trolling for you. :)

You obviously have no real experience in a serious programming
environment. Your code is sloppy and error-prone. I suspect your only
"experience" is writing sloppy PHP code for the web and hoping it will work.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Nov 10 '08 #14
Jessica Griego wrote:
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:gf**********@registered.motzarella.org...
>Jessica Griego wrote:
>>"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:gf**********@registered.motzarella.org...
Jessica Griego wrote:
<st***********@gmail.comwrote in message
news:e3**********************************@v39g 2000pro.googlegroups.com...
>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
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]);
}
>
>
Which provides incorrect results if it happens to cross an odd number of
daylight savings time changes.
Only if daylight savings time defined for the locale. A simple adjustment
can be made to offset the difference. No big deal.
Which does not mean your code is correct. It is not.

Sheldon's answer is much better. No adjustment required.

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.

:^)


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

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.
js*******@attglobal.net
==================
Nov 10 '08 #15
<st***********@gmail.comschreef in bericht
news:e3**********************************@v39g2000 pro.googlegroups.com...
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

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\ +${1}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.

Nov 11 '08 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
384
by: John | last post by:
Hi I am trying to calculate time difference in hours between two times. I am using the datediff function. The results are peculiar; ? DateDiff("h", "00:30", "6:30") returns 6, but ?...
4
31188
by: iamonthisboat | last post by:
I have a data set like so: UTC_TIME Timestamp NodeID Message Flag Line Station 11/19/2005 10:45:07 1132397107.91 1 3 5 1028 1034...
7
54905
by: walt | last post by:
Hello, I have been trying to calculate the difference between two date and display the difference in hours and minutes (HH:MM). I can't get it calculate properly and I can't hours and minutes to...
2
3169
by: cmrhema | last post by:
Hello I have a table GPSDATA which consists of VehicleNo,Speed,CurrentDate,slno,status This data gets updated every minute.And every minute the slno(serial no) will be incremented...
1
2711
by: shomai | last post by:
Is there a way for me to calculate time difference?
3
3419
by: Steve | last post by:
I am trying to calculate elapsed travel times for flights. My plan is to enter the local departure time, the departure city and the local arrival time and city. These times would be standardised...
3
3930
by: RN1 | last post by:
A Form has 2 TextBoxes where in users enter date & time. Example (in mm/dd/yyyy format): 09/20/2008 17:54 (1st TextBox) 09/29/2008 6:13 (2nd TextBox) How do I find out how much time (in...
2
3318
by: Guruganesh | last post by:
i have to calculate the time diference of two file names namely File 1: CBE03KMD_2010_03_08_23_06_36.upl File 2: CBE03KMD_2010_03_08_22_11_47.upl see the date is same but the time is also...
2
5290
by: bluemoon9 | last post by:
On my form, I have 3 fields: StartTime (milliary time 00:00), EndTime (milliary time 00:00) and Mintues (number). I would like to place a control calcuation statement in the Field Properties/Control...
0
7108
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7142
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6847
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5445
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4875
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
272
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.