Jump to content

Recommended Posts

I wrote a function that grabs the elapsed time of a recently uploaded video. However, the time does not seem to increment. For example, if I upload a video, the time will display as '1 second'.

However, if I continuously refresh the page, the time does not increment or increase. Any way to fix this? I figured I'd have to put it in some kind of loop (I do call the function in another class).

 

function getElapsedTime($time) {
	
	$time = time() - $time; // get time since video upload date
	$time = ($time < 1) ? 1 : $time;
	$tokens = array(
	    31536000 => 'year',
	    2592000  => 'month',
	    604800   => 'week',
	    86400    => 'day',
	    3600     => 'hour',
	    60       => 'minute',
	    1        => 'second'
	);
	
	foreach ($tokens as $unit => $text) {
	    if ($time < $unit) continue;
	    $numberOfUnits = floor($time / $unit);
	    return $numberOfUnits . ' ' . $text . (($numberOfUnits > 1) ? 's' : '');
    }
}

 

Keep in mind that PHP is server side and it is stateless. That means when you refresh the page, it is the same as issuing the page the first time unless you use sessions. It is not clear to me exactly what you are trying to accomplish but it perhaps you want to use javascript which is client side.

1 hour ago, requinix said:

You're return;ing inside the loop.

The whole function is actually kinda wrong. Try again. Without a loop.

I ended up copying the function from a StackOverflow answer. I am not familiar with some of the operations used in this function, but I will make an attempt to re-write it.

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.