Should be simple right? Well I've been at the PC for 16 hours so far and the answer is probably screaming at me in the face, but I can't hear it...

This code...
$differenceInSeconds = 448005;

if($differenceInSeconds >= "86400"){
    $calcDays = floor($differenceInSeconds / 86400);

    if($calcDays = "1"){
        $daysPlural = "day";
    }else{
        $daysPlural = "days";
    }

    $lastupdate = $calcDays." ".$daysPlural." ago";
}

echo $lastupdate;
Outputs this...
1 day ago
When it should output this...
5 days ago

Any ideas folks? Thanks,
Michael

try this i hope this work

<?php
$differenceInSeconds = 448005;

if($differenceInSeconds >= "86400"){
    $calcDays = floor($differenceInSeconds / 86400);
    //echo $calcDays;
    if($calcDays == "1"){
        $daysPlural = "day";
      //echo   $lastupdate = $calcDays." ".$daysPlural." ago";
    }else{
        $daysPlural = "days";
      //  echo $lastupdate = $calcDays." ".$daysPlural." ago";
    }

   $lastupdate = $calcDays." ".$daysPlural." ago";
}

echo $lastupdate;


?>
if ($calcDays = "1")

Is an assignment.....

This is why I like using "Yoda Conditions". Even if you make mistakes like these, the variable isn't overwritten.

if ("1" = $calcDays")

This would result in an actual error being thrown, since you can't assign to a constant value like that.

Also, why are you quoting the numbers in the IF statements? It's entirely pointless, and just adds the need for PHP to cast them back to numbers to do the comparison.

And you should initialize $lastUpdate before the main IF statement, or in an ELSE clause. Otherwise if the difference is less than a day, it'll be undefined.

Also I know this isn't important but perhaps you should remove the quotations from the integers/numbers to improve efficency. Comparing two numbers is a lot faster than comparing a number and a string because of the amount of bits stored and the conversion required by php. For example:

<?php
$differenceInSeconds = 448005;
if($differenceInSeconds >= 86400){
    $calcDays = floor($differenceInSeconds / 86400);
    //echo $calcDays;
    if($calcDays === 1){
        $daysPlural = "day";
      //echo   $lastupdate = $calcDays." ".$daysPlural." ago";
    }else{
        $daysPlural = "days";
      //  echo $lastupdate = $calcDays." ".$daysPlural." ago";
    }
   $lastupdate = $calcDays." ".$daysPlural." ago";
}
echo $lastupdate;

And note that the final php deliminator isn't required when dealing with a pure php page and will take off a few milliseconds off the page loading time.

haha knew it would be something stupid, cheers guys xD

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.