Jump to content

Recommended Posts

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 appreciated

Mark Willis
Link to comment
https://forums.phpfreaks.com/topic/32766-odd-timestamp-error/
Share on other sites

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 21640

then 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

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.