kram Posted January 3, 2007 Share Posted January 3, 2007 Hi all. Am currently working on a function to show the average amount off comments entered into the database since the first entry, up to the current day.What I have come up with so far is this... but i get an error saying "division by 0 on line 217".Can anyone shed some light onto my slightly mathematical problem. This is my code:[code] $currenttime = time(); $dateoffirstcomment = 1167888210 //$firstrow["commentdate"];//retrieved from DB $daysfromfirstcomment = abs($currenttime - $dateoffirstcomment); $daysfromfirstcomment = round($daysfromfirstcomment / 86400); //division by zero error for $daysfromfirstcomment if(strval($daysfromfirstcomment)=="") { $daysfromfirstcomment = 0; } $averagenumberofcomments = $numofcomments / $daysfromfirstcomment; // Line 217 return $averagenumberofcomments;[/code]My maths isn't amazing, any help would be greatly appreciatedMark Willis Link to comment https://forums.phpfreaks.com/topic/32766-odd-timestamp-error/ Share on other sites More sharing options...
genericnumber1 Posted January 3, 2007 Share Posted January 3, 2007 exactly what it says.. you can't divide by 0 it's mathematically impossible....[code] if(strval($daysfromfirstcomment)=="") { $daysfromfirstcomment = 0; }[/code]$daysfromfirstcomment is now 0[code]$averagenumberofcomments = $numofcomments / $daysfromfirstcomment; // Line 217[/code]you just tried to divide by 0......figure out why it's making $daysfromfirstcomment 0 and that will help fix your problem :D use echos and print_rs [b]EDIT[/b]I guess I'll help more than that...the current unix epoch time() = 1167866570~you're saying the first comment was in the future. (1167888210)but you "remedy" that by taking abs() which is 21640then you do 21640 / 86400 which will yield a decimal.. but you have round() which rounds the decimal to 0 Link to comment https://forums.phpfreaks.com/topic/32766-odd-timestamp-error/#findComment-152549 Share on other sites More sharing options...
Jessica Posted January 3, 2007 Share Posted January 3, 2007 Change it to 1 instead of 0. Link to comment https://forums.phpfreaks.com/topic/32766-odd-timestamp-error/#findComment-152553 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.