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

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Gunner
Posts: 276
Joined: Sun Aug 14, 2011 12:07 am

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

Post 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!
Last edited by Gunner on Sun Jan 19, 2014 5:48 am, edited 1 time in total.
Skype: mrvictordiaz
User avatar
Gunner
Posts: 276
Joined: Sun Aug 14, 2011 12:07 am

Re: How to use the server-side timer?

Post 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?
Skype: mrvictordiaz
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: How to use the server-side timer?

Post 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.
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Gunner
Posts: 276
Joined: Sun Aug 14, 2011 12:07 am

Re: How to use the server-side timer?

Post by Gunner »

MikuzA wrote:You could retrieve the $date set timestamp from your database or from your server.
How to do this?
With $_SERVER ?
Skype: mrvictordiaz
User avatar
Gunner
Posts: 276
Joined: Sun Aug 14, 2011 12:07 am

Re: How to use the server-side timer?

Post 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?
Last edited by Gunner on Mon Jan 13, 2014 2:16 pm, edited 1 time in total.
Skype: mrvictordiaz
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: How to use the server-side timer?

Post 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.
User avatar
Gunner
Posts: 276
Joined: Sun Aug 14, 2011 12:07 am

Re: How to use the server-side timer?

Post 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
Skype: mrvictordiaz
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: How to use the server-side timer?

Post 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.
User avatar
Gunner
Posts: 276
Joined: Sun Aug 14, 2011 12:07 am

Re: How to use the server-side timer?

Post 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?
Skype: mrvictordiaz
User avatar
Gunner
Posts: 276
Joined: Sun Aug 14, 2011 12:07 am

Re: How to use the server-side timer?

Post by Gunner »

And for adding times, do we just do the classic '+' operations? Or is there some sql function for this
Skype: mrvictordiaz
Post Reply

Return to “Beginner Help and Support”