Jump to content
xisto Community
SilverFox1405241541

Php Timer/delay Question

Recommended Posts

I'm not sure this is the right place to post this.But I'm working on a project and I need to know how to implement a timer.Like how would I make a delay between what the user says to do and the execution?Sorry if I'm not making sense.But what i was thinking is like.User Tells system to do something => System delays it using some formula for let's say like 512 Seconds => System Finishes what user said to do.Any tips on how to implement this?

Share this post


Link to post
Share on other sites

In my opinion, the sleep function isn't good for such things, due to when sleep is being used, the server doesn't do anything, if I understand what he wants, I would say to him to rather use AJAX technology for this kind of things and 512 seconds is really a lot, it is over 8 minutes, so having ajax do it is also not the best idea.. In fact, having such a delay, I think it isn't needed, you can store the timestamp in a database and check it..

Share this post


Link to post
Share on other sites

512 seconds is really a lot, it is over 8 minutes, so having ajax do it is also not the best idea.. In fact, having such a delay, I think it isn't needed, you can store the timestamp in a database and check it..

Here are some questions:

What is AJAX?

What do you mean store a timestamp?

How would I make the php script check the timestamp?

I'm asking b/c my project is a online virtual hacking game and I"ll need to implement timers for downloads and such. I know its possible as another game, Slavehack* has such a system.

 

*Slavehack, the slavehack name and game code is the property of its respective owner.

Share this post


Link to post
Share on other sites

I played a hell lot of SlaveHack when it had just started... As far as I remember, SlaveHack utilised JavaScript based timers along with a timestamp stored in MySQL DB.

 

The JS timer was displayed as a progress bar to the end-user. Right before the progress bar started animating, the current time would be written to the MySQL DB along with (I guess) the time taken to complete the ongoing operation. If the user interrupted it in between, all SH had to do was add the required time to the last registered timestamp and compare it with the current time. The difference would tell whether the user successfully completed the operation or aborted it.

 

To store a timestamp in the DB, you can use the in-built MySQL function called NOW(). The field where you store the timestamp has to be declared as of type TIME or DATETIME.

 

A typical timestamp will look like, 2007-05-08 8:30:21.

 

To insert the current timestamp into a MySQL field, issue the following statement:

INSERT INTO table ( time_field_name ) VALUES ( NOW() );
Keep in mind that this inserts a SECOND based timestamp i.e. the time lapses can be calculated from days, hours ... lowest upto the level of seconds.

 

However, if your game requires calculations at microsecond levels (or some other fraction of a second) - you'd prolly get down on your knees and mess around with the microtime() function of PHP.

 

Cheers,

m^e

Share this post


Link to post
Share on other sites

Yes, "sleep" is not usable. And Javascript for 8 minuts???!!! Why somebody must wait 8 minuts to download a file!!!!, I think you need to add a "song choice bar" to help the victims wait. KDE have "koffee", a tool that waits n minuts and then displays: "your coffee is ready!!" or some other custom message.

SilverFox, maybe your questions was made similar to the one I did when I was an student at the school. I needed an scanner for 50 photographs. Then I talked with the principal. He asked me: are you talking about 50,000 photographs??!!!, that will finish to kill our old scanner!!!. then I said: sorry, I was thinking on many photographs.... maybe 50.

Hotscripts.com must have many examples for "PHP Downloader" or "PHP Download System".

 

Spanish word of the day:

"retorica": tenses or phrases used by lawyers, politics, etc.

example: Hotscripts.com is property of its respective owners.

Human law is about waste your time and get paid!

Share this post


Link to post
Share on other sites

I'm not sure this is the right place to post this.
But I'm working on a project and I need to know how to implement a timer.

Like how would I make a delay between what the user says to do and the execution?

Sorry if I'm not making sense.

But what i was thinking is like.

User Tells system to do something => System delays it using some formula for let's say like 512 Seconds => System Finishes what user said to do.

Any tips on how to implement this?

The best way to do this is with a combination of javascript and php, in your php script if you wish you can set the initial time with the php time() function and the amount of seconds of your timer, then you simply add a javascript counter function inside the body tag of your document, check out this general counter:
<script type="text/javascript">	var myTime = 20;	function countDown() {		if (myTime == 0) {			// tasks to process when the time is over		}		else if (myTime > 0) { 			myTime--;			setTimeout("countDown()",1000);		}	}	countDown();</script>

Best regards,

Share this post


Link to post
Share on other sites

Thanks all for the help!I haven't gotten far enough to start trying to implement but I will have more important hurdles like this to overcome so I am sure I'll ask another question sometime down the ways.Also an off topic question, how do I set a timezone? Like I want it to show my timezone not the server timezone. How do I do that? I couldn't figure it out earlier.

Share this post


Link to post
Share on other sites

Uggh, timezones are evil. In fact, trying to get the server to deal with remote timezones can be a royal pain, sometimes impossible. I guess we'd need to know what you want to do with the timezone to be able to give you the best (or a good) way of handling them. For example, if the only the needed is to display the time in the user's timezone, then using javascript is probably your best bet, as that can draw from the local computer and generate a time in the current timezone automatically.~Viz

Share this post


Link to post
Share on other sites

Thanks all for the help!

 

I haven't gotten far enough to start trying to implement but I will have more important hurdles like this to overcome so I am sure I'll ask another question sometime down the ways.

 

Also an off topic question, how do I set a timezone? Like I want it to show my timezone not the server timezone. How do I do that? I couldn't figure it out earlier.

I agree with vizskywalker timezones are very nasty to manipulate or configure, one way is by using javascript by using the local time used by the user, now if you would use PHP5 you can set the default timezone by using the date_default_timezone_set() php function in your scripts or by using the date.timezone ini setting in your php.ini file. Also take a look to the Appendix I, List of Supported Timezones that are referenced on the PHP manual.

 

Best regards,

Share this post


Link to post
Share on other sites

Thanks all I will look into these and other issues soon.I just got back from a long road trip (not really only 50 miles away but I went in loops around the city B) you know how San Antonio can be :) If you don't your not missing much) and therefore am too exhausted to code tonight and *sigh* have to go to more s##t in the city later...gosh looks like coding's slowing down :)

Edited by SilverFox (see edit history)

Share this post


Link to post
Share on other sites

Ahm. Yes, I also have this question in mind. I have a beta-game (not yet finished) and I'm planning on making a mana system (simillar to Marcoland.com) wherein the mana refreshes every day. I'm not sure how to set it like that. (Am I allowed to ask questions as well?)Do I put a time stamp with the now() function that m^e has said and put an if statement when the time is equal?@Tavox Peru - That seems functional. I'll try that one out. I'm not sure how Javascript will get the time stamp from php though. I'm really confused with this subject.(LOL, I was thinking of a way to refresh the mana automatically... I said to myself, "Bah, I'll just refresh it manually. -.-;;" hehehe ")

Edited by lonelym (see edit history)

Share this post


Link to post
Share on other sites

What I'm thinking is, that you want to create a basic timestamp in mySQL.Setup your (perhaps javascript timer) to the appropriate time, and if the page changes or refreshes (or however you handle it) compare the original beginning timestamp, with a newly fresh calculated one to re-calculate the timer display.If they stay on the page, and the timer runs out, then you'll need to still do the timestamp query and comparison (to make sure they didn't mess with the JS code), and complete it, or if they come back to the page, and it's done, show it complete and carry on.Anyway, basically something along those lines I'm thinking.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.