calculating the difference between times

#1 juanc

juanc
• Members
• Member
• 16 posts

Posted 01 March 2006 - 05:39 PM

Hi can someone help me please,

My goal is to add to different times into two different mySQL columns (in the same row and inputted one after the other) then calculate the number of minutes in between each.

For each column I input using strftime('%H %M', time())

Then I used this function to calculate the difference

```
function DateDiff
(\$interval,\$date1,\$date2) {
// get the number of seconds between the two dates
\$timedifference = \$date2 - \$date1;

switch (\$interval) {
case 'w':
\$retval = bcdiv(\$timedifference,604800);
break;
case 'd':
\$retval = bcdiv(\$timedifference,86400);
break;
case 'h':
\$retval =bcdiv(\$timedifference,3600);
break;
case 'n':
\$retval = bcdiv(\$timedifference,60);
break;
case 's':
\$retval = \$timedifference;
break;

}
return \$retval;

}

```

Then I did

```
echo DateDiff('n',\$row[start],\$row[end]);
```

but it just gives back 0 .............at the same time I'll add that the mysql columns I originally had as varchar then changed them to time

hope someone can help

#2 XenoPhage

XenoPhage
• Members
• 99 posts

Posted 01 March 2006 - 05:43 PM

Why not use the mysql date functions? DATE_ADD and DATE_SUB ?

[a href=\"http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html\" target=\"_blank\"]http://dev.mysql.com/doc/refman/5.0/en/dat...-functions.html[/a]
--
[a href=\"http://blog.godshell.com\" target=\"_blank\"]XenoPhage[/a]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming.[/quote]

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users