Jump to content

Archived

This topic is now archived and is closed to further replies.

dalton6275

Calculation to find difference in minutes

Recommended Posts

Hey everyone,

Im trying to perform a date calculation to find the difference in minutes between 2 dates in MySQL.

currently i have:

[code]

<?php
while ($row=mysql_fetch_array($Recordset1))
{
$outagetime = $row_Recordset1['outage_end'] - $row_Recordset1['outage_start'];
echo number_format($outagetime, 3);
?>

[/code]

But i keep getting the result 0.000. I tried adding more decimals but it doesnt seem to be working and iv checked the 2 variables and there is clearly 90 minutes difference but the system keeps reporting 0.000.


Im baffled. Any ideas


Share this post


Link to post
Share on other sites
take a look at the examples, in your code you haven't identified the resource.

[a href=\"http://www.zend.com/manual/function.mysql-fetch-array.php\" target=\"_blank\"]http://www.zend.com/manual/function.mysql-fetch-array.php[/a]

Lite...

Share this post


Link to post
Share on other sites
ah, but im not sure if thats the entire problem. I cant seem 2 be able to perform any kind of date calculation from these mysql sources to find the difference in minutes. Ill read the page and see if that helps but i think i need to edit the date values somehow!

Share this post


Link to post
Share on other sites
what format are the columns in? if they are DATE or DATETIME columns, you can't perform regular arithmetic operations on it. if they are DATE/TIME columns, you can use some mysql functions to extract the difference in minutes between the two.

Share this post


Link to post
Share on other sites
yep, DATETIME format in MySQL. Only problem is i need the difference in minutes.

Can i turn these datetimes into a number then perform the calculation like that?

Share this post


Link to post
Share on other sites
[!--quoteo(post=362223:date=Apr 6 2006, 10:07 AM:name=dalton6275)--][div class=\'quotetop\']QUOTE(dalton6275 @ Apr 6 2006, 10:07 AM) [snapback]362223[/snapback][/div][div class=\'quotemain\'][!--quotec--]
yep, DATETIME format in MySQL. Only problem is i need the difference in minutes.

Can i turn these datetimes into a number then perform the calculation like that?
[/quote]

certainly... here's how i would go about it since it's the easiest to understand. keep in mind that strtotime() gives a UNIX timestampe that is the number of seconds since the UNIX epoch (12AM, Jan 1, 1970). with that in mind, we can do calculations... however, this script will ONLY work on dates that are AFTER Jan 1, 1970!
[code]
function getMinutesDiff($date1, $date2) {
  $date1 = strtotime($date1);
  $date2 = strtotime($date2);
  $diff = abs($date2 - $date1);

  $min = 60; // 60 seconds in a minute, obviously;-)
  
  return number_format(($diff / $min), 3);
}

while ($row = mysql_fetch_array($Recordset1))
  echo getMinutesDiff($row['outage_start'], $row['outage_end']);

[/code]

hope this helps

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.