Page 1 of 1

How to use the server-side timer? (solved)

Posted: Mon Jan 13, 2014 12:47 pm
by Gunner
So I found this code from the stackoverflow.

Code: Select all

<?php
$date = 'July 20 2011 05:00:00 PM PDT';
$exp_date = strtotime($date);
$now = time();

if ($now < $exp_date) {
?>
<script>
// Count down milliseconds = server_end - server_now = client_end - client_now
var server_end = <?php echo $exp_date; ?> * 1000;
var server_now = <?php echo time(); ?> * 1000;
var client_now = new Date().getTime();
var end = server_end - server_now + client_now; // this is the real end time

var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour *24
var timer;

function showRemaining()
{
    var now = new Date();
    var distance = end - now;
    if (distance < 0 ) {
       clearInterval( timer );
       document.getElementById('countdown').innerHTML = 'EXPIRED!';

       return;
    }
    var days = Math.floor(distance / _day);
    var hours = Math.floor( (distance % _day ) / _hour );
    var minutes = Math.floor( (distance % _hour) / _minute );
    var seconds = Math.floor( (distance % _minute) / _second );

    var countdown = document.getElementById('countdown');
    countdown.innerHTML = '';
    if (days) {
        countdown.innerHTML += 'Days: ' + days + '<br />';
    }
    countdown.innerHTML += 'Hours: ' + hours+ '<br />';
    countdown.innerHTML += 'Minutes: ' + minutes+ '<br />';
    countdown.innerHTML += 'Seconds: ' + seconds+ '<br />';
}

timer = setInterval(showRemaining, 1000);
</script>
<?php
} else {
    echo "Times Up";
}
?>
<div id="countdown"></div>
I want to make a Javascript countdown timer for my game, it seems 90% like the code above but I want to make it server-side, so that the user couldn't do anything with the timer as if it's client-side. I wonder if the code above is ran in the online hosts it will use the server time anyway is it? I just tried it on my localhost and it still uses my computer date. Even if I already added this on top of that
<? date_default_timezone_set('America/Chicago'); ?>
Help me, thanks!

Re: How to use the server-side timer?

Posted: Mon Jan 13, 2014 12:49 pm
by Gunner
BTW I'm really sick of those free hostings they said I have to wait for freaking 24 hours to get my account verified, in fact I just wanted to try all these cool stuff there. What the heck.

Question again, when we run those codes in our localhosts will the behavioer be the same as when we host them online? You got me right?

Re: How to use the server-side timer?

Posted: Mon Jan 13, 2014 12:55 pm
by MikuzA
Hello,

You could retrieve the $date set timestamp from your database or from your server.
So that the code above only 'simulates' the countdown.

And if people try to manipulate the result, it's still the same on refresh due the time is taken from the server.

and for you btw-question,
if you run them on localhost it should be the same. I would suggest XAMP/WAMP-solution.

Re: How to use the server-side timer?

Posted: Mon Jan 13, 2014 2:12 pm
by Gunner
MikuzA wrote:You could retrieve the $date set timestamp from your database or from your server.
How to do this?
With $_SERVER ?

Re: How to use the server-side timer?

Posted: Mon Jan 13, 2014 2:14 pm
by Gunner
BTW Mikuza can that retrieving thing be done in localhost? I couldn't seem to access any online hosts right now :?

OR what you meant is I have to host it in online hosts and not localhost straight away?

Re: How to use the server-side timer?

Posted: Mon Jan 13, 2014 3:26 pm
by hallsofvallhalla
Just place a timestamp in the db then everytime the server is hit it checks the current time to the timestamp if enough time has passed.

If you need a place to host something I can set you up. I have plenty of hosting to share. If you have a domain I can set you up with your own account, if not I can set up a sub domain.

Re: How to use the server-side timer?

Posted: Tue Jan 14, 2014 1:58 am
by Gunner
So if I have a building that needs 2 minutes to be done, is that timestamp thing I have to use?

I thought timestamps only changes when the values on a row change?
I don't get it how the countdown still technically runs with timestamps.

Maybe the prob is I don't fully understand how times on mysql works?

As for halls, I'm afraid with my self-consistency for now.. I don't wanna involve other people yet in my poor time-management I'm afraid that I will screw it up and not using it for time. Thanks for you offer man really appreciate that

Re: How to use the server-side timer?

Posted: Tue Jan 14, 2014 2:03 am
by hallsofvallhalla
all you are doing is comparing a current timestamp to the old one. Think of it like this. You want to time how long you have run. So before you leave the house you write down the current time. That is the db timestamp. You go run and when you get back you look at the current time. You then minus the current time from the time you wrote down and you have the total time you ran.

Re: How to use the server-side timer?

Posted: Tue Jan 14, 2014 3:11 am
by Gunner
OH I'm almost there.. So if we have a 2 min building that needs to be done, we just have to add 2 mins + the current timestamp, right? And how many columns for this thing will I need?
columns:
1. timestamp type: timestamp
2. just regular time column for applying the current date?

Re: How to use the server-side timer?

Posted: Tue Jan 14, 2014 3:50 am
by Gunner
And for adding times, do we just do the classic '+' operations? Or is there some sql function for this