phreak3r Posted August 21, 2020 Share Posted August 21, 2020 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' : ''); } } Quote Link to comment https://forums.phpfreaks.com/topic/311367-elapsed-time-does-not-increment-by-seconds-or-minutes/ Share on other sites More sharing options...
gw1500se Posted August 21, 2020 Share Posted August 21, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311367-elapsed-time-does-not-increment-by-seconds-or-minutes/#findComment-1580854 Share on other sites More sharing options...
phreak3r Posted August 21, 2020 Author Share Posted August 21, 2020 @gw1500se When you upload a video to YouTube it tells you 'uploaded x seconds ago' 'uploaded x minutes ago'. I am trying to implement that feature to display the elapsed time, since each file upload if that makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/311367-elapsed-time-does-not-increment-by-seconds-or-minutes/#findComment-1580861 Share on other sites More sharing options...
requinix Posted August 21, 2020 Share Posted August 21, 2020 You're return;ing inside the loop. The whole function is actually kinda wrong. Try again. Without a loop. Quote Link to comment https://forums.phpfreaks.com/topic/311367-elapsed-time-does-not-increment-by-seconds-or-minutes/#findComment-1580866 Share on other sites More sharing options...
phreak3r Posted August 22, 2020 Author Share Posted August 22, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311367-elapsed-time-does-not-increment-by-seconds-or-minutes/#findComment-1580873 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.