457,995 Members | 1,253 Online Need help? Post your question and get tips & solutions from a community of 457,995 IT Pros & Developers. It's quick & easy.

# calculate time difference in mintues

 P: n/a 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 Replies

 P: n/a

 P: n/a On Nov 9, 12:56*am, "Jessica Griego"

 P: n/a 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

 P: n/a Jessica Griego 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 intervaldifference in minutes? I played with strtotime() but but that onlygave 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

 P: n/a "Jerry Stuckle" >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 intervaldifference in minutes? I played with strtotime() but but that onlygave 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

 P: n/a Jessica Griego wrote: "Jerry Stuckle" Jessica Griego wrote: >>

 P: n/a "Jerry Stuckle" "Jerry Stuckle" >Jessica Griego wrote: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 intervaldifference in minutes? I played with strtotime() but but that onlygave me difference in hours and only if the times were on the same day(after wrapping with date() function). TIAfunction 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 ofdaylight savings time changes. Only if daylight savings time defined for the locale. A simple adjustmentcan 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

 P: n/a "Jerry Stuckle" "Jerry Stuckle" >Jessica Griego wrote: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 intervaldifference in minutes? I played with strtotime() but but that onlygave me difference in hours and only if the times were on the same day(after wrapping with date() function). TIAfunction 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 ofdaylight savings time changes. Only if daylight savings time defined for the locale. A simple adjustmentcan 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 