Page 1 of 1
					
				Displaying Minutes and Seconds left.
				Posted: Tue Feb 21, 2012 11:09 pm
				by vitinho444
				Hey aLL!!
So... (f*ck i always start a post with So... -.-)
Im still making my browser game 

(yayyy) and i got the construction system working properly (double yayyy)!
And now.. i face the biggest problem of my life... nahh just kidding.. well im kinda trying to make a system or something that allows me to display how much time left to build the building. like: HOUR:MINUTES:SECONDS or just MINUTES:SECONDS.
My time system its that one 
 i store the seconds that passed from 1970 i think till now plus the build time and then keep checking how much is left to the actual time(); become bigger than the other.
And till now i got a display in seconds 
Any ideas?
 
			
					
				Re: Displaying Minutes and Seconds left.
				Posted: Wed Feb 22, 2012 2:12 am
				by Ark
				Well i'll tell you the way I do it..
I store the time it will finish the building in teh db
column: time_to_finish
type: datetime
then if the player gets on the page at any time before the building finishes.
Code: Select all
$query=mysql_query("SELECT `time_to_finish` FROM `table` WHERE `building`='$building_id'") or die();
$result=mysql_fetch_assoc($query);
$sec_to_finish=strtotime($result['time_to_finish']) - time(); // time_to_finish is in the future so it's greater than current time.
if($sec_to_finish <= 0){ 
// the building has finished
}
else{
// echo the *countdown
}
 
I have a countdown at 
this post  which you can display the H:M:S left until the building finishes.
 
			
					
				Re: Displaying Minutes and Seconds left.
				Posted: Wed Feb 22, 2012 7:20 pm
				by vitinho444
				i didnt get it.. how can i show the minutes and seconds?
			 
			
					
				Re: Displaying Minutes and Seconds left.
				Posted: Wed Feb 22, 2012 8:39 pm
				by Chris
				Found this: 
http://pure-essence.net/2005/05/05/time ... -function/
Code: Select all
$currentTime = time();
$futureTime = time() + 12348234;
$difference = timeleft( $currentTime, $futureTime );
foreach( $difference as $key => $amount )
{
    echo ( $key > 0 ? ':' : '' ) . $amount;
}
 
 
			
					
				Re: Displaying Minutes and Seconds left.
				Posted: Wed Feb 22, 2012 9:28 pm
				by vitinho444
				EDIT 3
FINALLY IM SO DUMB XD 
i got it 
 
Working very nice.
just one more thing, its possible to refresh the time only? (not the entire page because i got a $_POST ...)
 
			
					
				Re: Displaying Minutes and Seconds left.
				Posted: Wed Feb 22, 2012 9:32 pm
				by Chris
				Can I see your code?
			 
			
					
				Re: Displaying Minutes and Seconds left.
				Posted: Wed Feb 22, 2012 9:40 pm
				by vitinho444
				Code: Select all
			$currenttime = time();
			$futuretime = $tempoc;
			
			$difference = timeleft($currenttime, $futuretime);
			echo "<center>" . $lang['Construindo'][$language] . " ";
			foreach($difference as $key => $amount)
			{
				echo ($key > 0 ? ':' : '') . $amount;
			}
			echo "</h4>";
its working properly 
just want to refresh the time every second (just the time 

)
 
			
					
				Re: Displaying Minutes and Seconds left.
				Posted: Wed Feb 22, 2012 10:03 pm
				by Chris
				You'll need JavaScript to do this.
My suggestion would be to calculate the time change in JavaScript aswell.
Try this:
Code: Select all
<span id="timer">
</span>
<script type="text/javascript">
var currentTime = <?php echo time(); ?>,
    futureTime = <?php echo $tempoc; ?>,
    timerSpan = document.getElementById('timer');
function timeLeft(current, end)
{
    var days = 0,
        hours = 0,
        minutes = 0,
        seconds = 0,
        left = end - current;
    if( left > 0 )
    {
        days = Math.floor(left / 86400);
        left -= days*86400;
        hours = Math.floor(left / 3600);
        left -= hours*3600;
        minutes = Math.floor(left/60);
        seconds = left - minutes * 60;
    }
    return days + ':' + hours + ':' + minutes + ':' + seconds;
}
setInterval( function()
{
    currentTime++;
    timerSpan.innerHTML = timeLeft(currentTime, futureTime);
}, 1000 ); // loop every second
</script>
 
			
					
				Re: Displaying Minutes and Seconds left.
				Posted: Thu Feb 23, 2012 1:40 pm
				by vitinho444
				where i put it and how i display only the time left?
			 
			
					
				Re: Displaying Minutes and Seconds left.
				Posted: Thu Feb 23, 2012 2:04 pm
				by Chris
				The script shows the time left inside the element with the id "timer", in my previous example I used a span tag. I noticed you are closing a h4 in your code so my guess is you want to achieve this (note this isn't the ideal means of achieving this):
Code: Select all
         echo '<h4 style="text-align: center">' . $lang['Construindo'][$language] . "</h4>";
         echo '<h4 id="timer"></h4>';
?>
<script type="text/javascript">
var currentTime = <?php echo time(); ?>,
    futureTime = <?php echo $tempoc; ?>,
    timer = document.getElementById('timer');
function timeLeft(current, end)
{
    var days = 0,
        hours = 0,
        minutes = 0,
        seconds = 0,
        left = end - current;
    if( left > 0 )
    {
        days = Math.floor(left / 86400);
        left -= days*86400;
        hours = Math.floor(left / 3600);
        left -= hours*3600;
        minutes = Math.floor(left/60);
        seconds = left - minutes * 60;
    }
    return days + ':' + hours + ':' + minutes + ':' + seconds;
}
setInterval( function()
{
    currentTime++;
    timer.innerHTML = timeLeft(currentTime, futureTime);
}, 1000 ); // loop every second
</script>
<?php
// continue php script