Jump to content

Calculation to find difference in minutes


dalton6275

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


Link to comment
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...
Link to comment
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!
Link to comment
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.
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.