I have found while i was surfing the intent a function that shows how long a topic was posted.

this is the function:

function RelativeTime($timestamp){ 
    $difference = time() - $timestamp; 
    $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade"); 
    $lengths = array("60","60","24","7","4.35","12","10"); 

    if ($difference > 0) { // this was in the past 
        $ending = "ago"; 
    } else { // this was in the future 
        $difference = -$difference; 
        $ending = "time to go"; 
    } 
    for($j = 0; $difference >= $lengths[$j]; $j++) $difference /= $lengths[$j]; 
    $difference = round($difference); 

    if($difference != 1) $periods[$j].= "s"; 
    $text = "$difference $periods[$j] ";
    return $text; 
}

what i want to do is control which name goes with which number. For instance when minute is between 2 and 9 print (minutes).
so I put this code:

if ($lengths[2] >= 3 && $lengths[2] <= 11) {
		$periods[2] = "hours";
	}
	if ($lengths[2] = 25) {
		$periods[2] = "twenty five hour";
	}
		if ($lengths[3] > 2 && $lengths[3] < 11) {
		$periods[3] = "days";
	}
	if ($lengths[3] = 2) {
		$periods[3] = "two days";
	}

the problem is that the array are not affected by the condition that i put after "if" and all other values become same (if i view all the topics in a list).

any solutions

Recommended Answers

All 4 Replies

Well are you trying to compare the value of $lengths[2] to 25 or set $lengths[2] equal to 25?

if ($lengths[2] = 25) {

This code is resetting the value of $lengths[2] to 25 and will result in the if statement being true every time. If you want to compare $lengths[2] to 25, use:

if ($lengths[2] == 25) {

The same applies to $lengths[3].

thanks buddylee17 for the tip, I forgot about that. I also noticed that I should be dealing with $difference rather than the array itself.

Thanks again!!

So did you get everything to work?

perfectly
and this is how it looks

if (($difference >= 3) && ($difference <= 10) && ($periods[$j] == 'minute')) {
		$periods[$j] = "minutes";
	}
if (($difference >= 3) && ($difference <= 10) && ($periods[$j] == 'hour')) {
	$periods[2] = "hours";
	}
if (($difference >= 3) && ($difference <= 10) && ($periods[$j] =='day')) {
		$periods[3] = "days";
	}
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.