Jump to content

Is it posible?


phpfan101

Recommended Posts

So, on my website, i have added a "last active" feature, so if someone is surfing the site, they can view when another users last page visit was.

 

so, every second, +1 is added to the $ts variable

 

what i have now, working and all, shows ONLY "NOW!", or ONLY "1 second", ONLY "___ seconds ago"... and so on

 

I have tried over, and over, and over to fix this to my liking, with more instances available, as an example, my current script will show somebody who is 13 minutes and 26 seconds inactive, just as "13 Minutes", i want to see the "26 seconds" too... some thing "Last Active: 13 Minutes and 26 seconds ago"

 

function howlongtil($ts) {
   $ts=$ts - time();
   if ($ts<1)
       // <1 second
       return " NOW";
   elseif ($ts==1)
       // <1 second
       return $ts." second";
   elseif ($ts<60)
       // <1 minute
       return $ts." seconds";
    elseif ($ts<120)
       // 1 minute
      return "1 minute";
   
    else 
       // <1 hour
       return floor($ts/60)." minutes";
      
};

 

So This is what i've got, anyone got a clue as to how i can amend it to show all the differences?

Link to comment
Share on other sites

I used this function to display time remaining until an online auction finishes, which is typically days, hours, mins and secs. It does not handle leap years or months, but it may be sufficient for what you need:

 

<?php 


$from_tstamp = mktime(0, 0, 0, 2, 1, 2009);
$to_tstamp = mktime(0, 0, 2, 2, 1, 2009);

var_dump(timestamp_difference_units_to_string(timestamp_difference_to_units($from_tstamp, $to_tstamp)));



function timestamp_difference_to_units($from_tstamp, $to_tstamp){

$from_tstamp = intval($from_tstamp);
$to_tstamp = intval($to_tstamp);

$seconds_remaining = $to_tstamp-$from_tstamp;
if($seconds_remaining < 0){
	$retval = timestamp_difference_to_units($to_tstamp, $from_tstamp);
	foreach($retval as $k=>$v){
		$retval[$k] = -$v;
	}
	return $retval;
}

$num_years = 0;
$num_days = 0;
$num_hours = 0;
$num_mins = 0;
$num_secs = 0;

while($seconds_remaining >= 31536000){
	$num_years++;
	$seconds_remaining -= 31536000;
}

while($seconds_remaining >= 86400){
	$num_days++;
	$seconds_remaining -= 86400;
}

while($seconds_remaining >= 3600){
	$num_hours++;
	$seconds_remaining -= 3600;
}

while($seconds_remaining >= 60){
	$num_mins++;
	$seconds_remaining -= 60;
}

$num_secs = $seconds_remaining;

$seconds_remaining = 0;

return array(
	'years' => $num_years,
	'days' => $num_days,
	'hours' => $num_hours,
	'minutes' => $num_mins,
	'seconds' => $num_secs
);

}


function timestamp_difference_units_to_string($diff){

$parts = array();

if($diff['years']>0){
	$parts[] = $diff['years'].' years';
}

if($diff['days']>0){
	$parts[] = $diff['days'].' days';
}

if($diff['hours']>0){
	$parts[] = $diff['hours'].' hours';
}

if($diff['minutes']>0){
	$parts[] = $diff['minutes'].' mins';
}

if($diff['seconds']>0){
	$parts[] = $diff['seconds'].' secs';
}

$last_part = array_pop($parts);

$retval = implode(', ', $parts);
if(count($parts)>0){
	$retval .= ' and '.$last_part;
}else{
	$retval .= $last_part;
}

return $retval;

}

Link to comment
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.