tags:

views:

48

answers:

2

Want to display a different message if the time is under a certain amount. I have then function that I have been working on, but cannot not get it to work unless the if for time is 1. Would like 10 minutes.

function TimeSince($timestamp)
    {
    // array of time period chunks
    $chunks = array(
        array(60 * 60 * 24 * 365 , 'year'),
        array(60 * 60 * 24 * 30 , 'month'),
        array(60 * 60 * 24 * 7, 'week'),
        array(60 * 60 * 24 , 'day'),
        array(60 * 60 , 'hour'),
        array(60 , 'minute'),
    );

    // difference in seconds
    $since = time() - $timestamp;

    // calculate one chunk of time
    for ($i = 0, $j = count($chunks); $i < $j; $i++)
        {
        $seconds = $chunks[$i][0];
        $name = $chunks[$i][1];

        // finding the biggest chunk (if the chunk fits, break)
        if (($count = floor($since / $seconds)) != 0)
            {
            break;
            }
        }

    // set output var
    $output = ($count == 1) ? '1 '.$name : "$count {$name}s";

    // Displays time of if under 10 minutes displays Just Now
  if($output < 10) {
    return ("Just Now!");
  }
    else {
          return ($output . " ago");
        }   
    return $output;
    }
A: 

You're setting $output to a string value in this line:

$output = ($count == 1) ? '1 '.$name : "$count {$name}s";

And then attempting to compare it to a number. Try assigning a numeric value to $output.

JCD
+1  A: 

In the following line of code:

$output = ($count == 1) ? '1 '.$name : "$count {$name}s";

You redefine $output as a string, consider checking before you assign it as one such as:

// Displays time of if under 10 minutes displays Just Now
if($count < 10) {
  return ("Just Now!");
}
  else {
  // set output var
  $output = ($count == 1) ? '1 '.$name : "$count {$name}s";
  return ($output . " ago");
}   
Nullw0rm
Tried that with the same results.
timg
Did you get rid of the $output = ($count.... code you set before?'Also change $output to $count in the IF statement
Nullw0rm
Yes I did both of those things and still not working
timg
$output is just a text.. you cant use it to know it 10 min have passed or not, your $count is like a flag to find the chunk.. you have to use your $since variable like @Raoul Duke did it if you want to verify the 10 minutes
pleasedontbelong
ok I got it, thanks.
timg
A working example is here:http://pastie.org/private/eqkua8vcfgvaqwxnqeijq Please accept the answer if it works!
Nullw0rm