Displaying Minutes and Seconds left.

C++, C#, Java, PHP, ect...
Post Reply
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Displaying Minutes and Seconds left.

Post by vitinho444 »

Hey aLL!!

So... (f*ck i always start a post with So... -.-)

Im still making my browser game :D(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

Code: Select all

time();
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

Code: Select all

FINISH_TIME - time()
Any ideas?
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

Re: Displaying Minutes and Seconds left.

Post 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.
Orgullo Catracho
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: Displaying Minutes and Seconds left.

Post by vitinho444 »

i didnt get it.. how can i show the minutes and seconds?
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: Displaying Minutes and Seconds left.

Post 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;
}
 
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: Displaying Minutes and Seconds left.

Post by vitinho444 »

EDIT 3

FINALLY IM SO DUMB XD

i got it :D

Working very nice.

just one more thing, its possible to refresh the time only? (not the entire page because i got a $_POST ...)
Last edited by vitinho444 on Wed Feb 22, 2012 9:33 pm, edited 1 time in total.
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: Displaying Minutes and Seconds left.

Post by Chris »

Can I see your code?
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: Displaying Minutes and Seconds left.

Post 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 :D)
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: Displaying Minutes and Seconds left.

Post 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>
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: Displaying Minutes and Seconds left.

Post by vitinho444 »

where i put it and how i display only the time left?
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: Displaying Minutes and Seconds left.

Post 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
Fighting for peace is declaring war on war. If you want peace be peaceful.
Post Reply

Return to “Coding”