dalton6275 Posted April 6, 2006 Share Posted April 6, 2006 Hey everyone, Im trying to perform a date calculation to find the difference in minutes between 2 dates in MySQL.currently i have:[code]<?phpwhile ($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 Quote Link to comment Share on other sites More sharing options...
litebearer Posted April 6, 2006 Share Posted April 6, 2006 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... Quote Link to comment Share on other sites More sharing options...
dalton6275 Posted April 6, 2006 Author Share Posted April 6, 2006 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! Quote Link to comment Share on other sites More sharing options...
obsidian Posted April 6, 2006 Share Posted April 6, 2006 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. Quote Link to comment Share on other sites More sharing options...
dalton6275 Posted April 6, 2006 Author Share Posted April 6, 2006 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 Link to comment Share on other sites More sharing options...
obsidian Posted April 6, 2006 Share Posted April 6, 2006 [!--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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.