PHP - How To Change Seconds Into Days, Hours, Minutes, Seconds
Hi, echo '<div style="float:right;width:60%;color:#666666">'. TeamSpeak3_Helper_Convert::version($ts3->virtualserver_uptime) .'</div>'; Edited June 24, 2020 by Lux05 Similar TutorialsThis topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=352669.0 Other than if statement, is there a way to change a duration give in seconds (e.g. 5746 seconds) to the standard format of x hours and y minutes and z seconds? 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' : ''); } }
Unfortunately I do not know how to go about doing this in php any simple examples of time and date will be great. So what I am trying to do is, display to minutes until the next half hour as well as the seconds until the next minute. Eg. Code: [Select] <?php echo $minutes; ?>:<?php echo $seconds; ?> Any help using the time and or date functions in this case will be great. Thank you for your stupendous help. Brian This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=342245.0 so i have a mysql column with a datetime and i was wondering how i could make it echo the time in "X minutes ago" or "X hours ago" and also, when its more then 24 hours old, delete the entire entry How do I echo the time in SECONDS? $tz = new DateTimeZone('America/Chicago'); $date = new DateTime('now', $tz); $mtime = $date->format('Y-m-d H:i:s'); echo ($mtime); E.g. 1296057739 I am trying to write a script that is going to access a database, grab the newest picture from it, find the height and width, analyze the height and width to find which command (which is assigned to a variable) is the one to run, run that command in the terminal, and then access the database again 6 seconds later (new pictures are uploaded every 6 seconds) and re-run and enter the command for this new picture. I have most of this already done, the problem I am having is figuring out how to get this to run every 6 seconds for forever. (Yes it has to be every 6 seconds.) Can anyone help me? Thanks in advance for the help! I'd like to make an sql request every x seconds with php. is it possible? I need to get the +10 timezone. But 1. The output for +10 is incorrect. Please help to fix it. 2. Is this a good way to get it? <?php echo "time=" (.time() + 36000); ?> Hi! I have got a problem with this function: Code: [Select] <?php function Sec2Time($time){ if(is_numeric($time)){ $value = array( "years" => 0, "days" => 0, "hours" => 0, "minutes" => 0, "seconds" => 0, ); if($time >= 31556926){ $value["years"] = floor($time/31556926); $time = ($time%31556926); } if($time >= 86400){ $value["days"] = floor($time/86400); $time = ($time%86400); } if($time >= 3600){ $value["hours"] = floor($time/3600); $time = ($time%3600); } if($time >= 60){ $value["minutes"] = floor($time/60); $time = ($time%60); } $value["seconds"] = floor($time); return (array) $value; }else{ return (bool) FALSE; } } ?> and my output for example: echo Sec2Time(604800); returns = "Array" ?? What I am wanting is to return a time from how many seconds. 604800 is 7 days so I want it to say 7 days. 2419200 is 1 month so I want it to say 1 month or 4 weeks. is (above) the right function for what I want ? thanks. Hello, I'm working on a simple project so bear with me, I have a variable called 'wood' on my SQL database, and I want to somehow increase the 'wood' value (int) every 5 seconds... Even when the user is not currently view that page. So what I'm thinking is saving the current mktime() to my database once - for sake of argument, this is going to be called 'time_cache', as well as the wood(int) variable. Then, when the user logs in to see how much wood there is, the PHP will, take the mktime() and subtract the 'cache_time' to see how many seconds have passed... Now I want to increase the wood int by 1 every 5 seconds that have passed. So I guess I need to do a division such as $seconds_passed = (mktime() - $time_cache) / 5. Which will give me the amount of 5 seconds that have passed, right? Then, surely my $wood will just equal $seconds_passed? I'm not sure if this is the best way to do something like this? Any one else have an idea? Also, my wood value doesn't 100% work as it give a decimal... I just want the wood value to increase by 1, every 5 seconds. Hi, I want to update a row when 30sec are passed on a page. If not nothing happens(not updating row). Example: You go to example.php>> 30sec pass, update row>> 30sec not over, no update. it does not necessarily have to be second it can be minutes or hours. Who can help me? Hope its clear enough, Thanks. Have started to program my own cricket management game and wish to have the database update every 60 seconds; this is to facilitate players who are not watching the actual game. Games are starting (and finishing) at all times so it is not possible to allow the user to be the only one to activate the game code; it is also not possible to only play the games when the human players are online because other games depend on the results. The only answer I can think of at the moment is to have a copy of firefox running on the server which runs the PHP code to update all current games and have this page automatically refresh every 60 seconds. Is this even viable? Any other suggestions? James I think I'm seriously missing some small thing to get this code to just auto refresh after (x) seconds or after video completes fully. This is for users to watch from a database of other users that submitted url's - right now all I could put a skip button to auto-refresh. If anyone can help or at least point me into the right direction on some snippets I can kind-of work off of. here is the snippet (kind-of a big snippet) Code: [Select] <? include('header.php'); foreach($_GET as $key => $value) { $secure[$key] = filter($value); } if($_GET['a'] == "skip"){ $sit1 = mysql_query("SELECT * FROM `youtube` WHERE `id`='{$secure['id']}'"); $sit = mysql_num_rows($sit1); if($sit > 0){ mysql_query("INSERT INTO `viewed` (user_id, site_id) VALUES('{$data->id}','{$secure['id']}')"); }} ?> <div class="block medium right"> <div class="top"><?if(isset($data->login)) {?> <h1>Earn Coins - Youtube</h1> </div> <div class="content"> <? // fetches all "unplayed" youtube videos for this user // $site2 = mysql_query("SELECT * FROM `youtube` WHERE (`active` = '0' AND `points` >= `cpc`) AND `id` NOT IN (SELECT `site_id` FROM `viewed` WHERE `user_id`='{$data->id}') ORDER BY `cpc` DESC LIMIT 0, 1"); $site = mysql_fetch_object($site2); $ext = mysql_num_rows($site2); if($ext > 0){ ?> <script src="js/swfobject.js"></script> <script type="text/javascript"> var playing = false; var fullyPlayed = false; var interval = ''; var played = 0; var length = 30; function YouTubePlaying(){ played += 0.1; roundedPlayed = Math.ceil(played); document.getElementById("played").innerHTML = Math.min(roundedPlayed,length); if (roundedPlayed == length){ if (fullyPlayed == false){ YouTubePlayed(); fullyPlayed = true; } } } function YouTubePlayed(){ var response = '<? echo $site->youtube;?>'; var username = "<? echo $data->id;?>"; $.post("ytreceive.php", { data: response + "---" + username}, function(result){ if(!isNaN(result)) { var curr_val = $('#points').text(); var new_val = parseInt(curr_val)+parseInt(result); $('#points').text(new_val); } } ); document.getElementById(response).style.visibility = "visible"; } function onYouTubePlayerReady(playerId){ ytplayer = document.getElementById("myytplayer"); ytplayer.addEventListener("onStateChange", "onYouTubePlayerStateChange"); } function onYouTubePlayerStateChange(newState){ if (newState == 1){ playing = true; interval = window.setInterval('YouTubePlaying()',100); }else{ if (playing) window.clearInterval(interval); playing = false; } } function refreshpage() { window.location.reload(); } </script> <center> <div style="width: 520px; padding: 10px;"> <h3>"<? echo $site->title;?>"</h3> View this video for 30 seconds and after that you will receive <? echo $site->cpc;?> coins<br/><br/> <div id="ytPlayer">You need Flash player 8+ and JavaScript enabled to view this video.</div> <script type="text/javascript"> var params = { allowScriptAccess: "always" }; var atts = { id: "myytplayer" }; swfobject.embedSWF("http://www.youtube.com/v/<? echo $site->youtube;?>?enablejsapi=1&playerapiid=ytplayer&autoplay=1", "ytPlayer", "425", "356", "8", null, null, params, atts); </script> <br/> <br />Must play for <span id="played">0</span>/10 seconds (<a href="youtube.php?a=skip&id=<? echo $site->id;?>" style="color:blue">Skip</a>) <div id="<? echo $site->youtube;?>" style="visibility:hidden"><a href="javascript:refreshpage()">View Next Video</a></div> </div></center> <?}else{?> <div class="msg"> <div class="error">Sorry, there are no more coins to be earned at the moment. Please try again later.</div> <div class="info"><a href="buy.php"><b>Feel like you need more coins? You can purchase them now!</b></a></div></div> <?}}else{?><script>document.location.href='index.php'</script><?}?> </div> </div> <?include('footer.php');?> I have a client who wants to use an animated GIF as his logo. It's a pretty cool animation but once the animation stops, the "static" logo isn't very clear. Is there some way (with either PHP or any way really) to have an image be replaced after a certain amount of time (say 5 seconds) with another image? In other words, when a page loads, the animated GIF will load first and after 5 seconds of that initial animation, the GIF logo will switch to a much clearer PNG logo file. Yes, I know I should just learn Flash and recreate the animation in Flash, but I don't have that ability right now Thanks everyone! Hi all, I have a database with some timestamps (in seconds) in it. Now I want to reverse it to normal readable time and date formats. Will someone tell me how please. Thnx Ryflex Hi. I have a query that determines if user is online/offline. Works perfectly when page loads. I want to change to auto run query every X seconds. NOTES: - The query is on a .php page with multiple other queries. -IF it is possible to still run the query with-in this page? or make a separate file and include in an iframe? i would like to avoid the iframe and just $output the result of query every X seconds. Here is my $query code: $query = "SELECT `manager_id` FROM #__profiles_xref WHERE profileid = '$profid'"; $onofflineid=doSelectSql($query,1); foreach ($onofflineid as $isonoffline){ $profmanagerid=$isonoffline->manager_id; } $query = "SELECT `session_id` FROM table_session WHERE userid = '$propmanagerid'"; $onofflinestatus=doSelectSql($query,1); if (count($onofflinestatus)>0) { $output['PROPSTATUS']='Online'; } else { $output['PROPSTATUS']='Offline'; } Ok Let's say I have a timestamp: 1327482941 I want to convert this to output only "60" im not looking for a whole time_ago function, simply just need to convert a timestamp down to seconds ONLY not having minutes/years/etc no need none of that just purely seconds starting from 1-60 if so how is it possible? I tried to just Code: [Select] $seconds = substr($e,1,4) . ""; $e is my timestamp, I did this because I could just grab the last 2 digits in the timestamp that would be what I want, but that's not possible because it goes 1-90 not 1-60 ^_^ help Here is a code I am trying to make it only spit out 1-60 in seconds Code: [Select] function timeAgo4($timestamp, $granularity=1){ $difference = $timestamp; if($difference < 0) return '0'; elseif($difference < 86400){ $periods = array(60,'' => 1); $output = ''; foreach($periods as $key => $value){ if($difference >= $value){ $time = round($difference / $value); $difference %= $value; $output .= ($output ? ' ' : '').$time.' '; $granularity--; } if($granularity == 0) break; } return ($output ? $output : '0').''; } else return date($format, $timestamp); } Code: [Select] $startdt = $_POST['startdt'].":".$_POST['starttmh'].":".$_POST['starttmm'].":".$_POST['sAMPM']; // 09/08/2011:11:35:AM $enddt = $_POST['enddt'].":".$_POST['endtmh'].":".$_POST['endtmm'].":".$_POST['eAMPM']; //09/09/2011:11:35:AM From the code above I want to subtract 09/08/2011:11:35:AM from 09/09/2011:11:35:AM and get the total number of hours. A simple solution would be highly appreciated: thanks in advance: |