Jump to content

Recommended Posts

Hi.:) I have a question about PHP and times with MySQL I am trying to program something here that will have a time limit when you are doing something. Let's say I am giving the user 5 minutes to complete a task. When the user hits the submit button to start the task, the 5 minute timer will start. Then from there the 5 minutes will count down. I also would like to display a counter to the user in the format of minute and seconds. so, like 5:00. And if he/she comes back to the page and is out of time, I want it to display "sorry, you did not complete my task in time!" If they have more time left, then I want the time remaining, along with a submit button so they can complete the task. Also, if they have met the requirements when they hit the submit button,the timer will end, and it goes back to the original "do you want to start a new task?" form submit button. How would I go about doing the whole time thing? I know how to do the submit button, I am good with simple forms... not so much advance, because I'm having trouble with a few forms, but times I do have trouble with Basically, what is the function name, or what is this called in PHP? I'm not sure if I need actual codes form you guys, but maybe you can tell me what this is called... because I've googled so many different time things, and I'm not finding what I'm looking for. Thank you in advance! ^_^

Link to comment
https://forums.phpfreaks.com/topic/272958-php-mysql-times/
Share on other sites

The way I would tackle this is by using a cookie.

 

When the user hits the start button I would set a cookie that contains the time that the button was clicked. You can then read this value and add 5 minutes to it to get your expiry time. If the current time is greater than the expiry time you will know that the user has ran out of time. To display a counter you will need to use javascript and a little bit of ajax to get the required data from the server. By using a cookie, a user can leave your website, close their web browser, etc, come back and the data will still be available.

 

PHP has lots of time functions. For instance to get the current time as a unix timestamp, simply use time().

 

All of PHP's date & time functions are listed on the left of the manual page

http://php.net/time

 

What you are trying to do will require a decent knowledge of PHP / JS / AJAX

Edited by neil.johnson
Link to comment
https://forums.phpfreaks.com/topic/272958-php-mysql-times/#findComment-1404734
Share on other sites

I wouldn't do it that way, as it's way too easy to manipulate a cookie.

 

For this I'm going to assume that you're using a login-system, so that you can verify your users in that manner. What I'd do then, is to save the starting time in the database, and check this upon (re-)entry of the page. Should said timer be older than now + timeout, then show them the "sorry" message.

To get the counter on the page, just send the start time and timeout to a JS, and use it to display the counter.

Link to comment
https://forums.phpfreaks.com/topic/272958-php-mysql-times/#findComment-1404737
Share on other sites

I wouldn't do it that way, as it's way too easy to manipulate a cookie.

 

For this I'm going to assume that you're using a login-system, so that you can verify your users in that manner. What I'd do then, is to save the starting time in the database, and check this upon (re-)entry of the page. Should said timer be older than now + timeout, then show them the "sorry" message.

To get the counter on the page, just send the start time and timeout to a JS, and use it to display the counter.

 

If you are going to store the start time in a database then you still require an identifier for the user upon returning to the page. This is not a problem if you must login prior. This is why I suggested using a cookie.

Link to comment
https://forums.phpfreaks.com/topic/272958-php-mysql-times/#findComment-1404911
Share on other sites

Seems like I wasn't specific enough in my opening line. What I meant to say was that I would not store the time in the cookie, as the user could easily defeat the system in that case.

 

Using a cookie to identify the user, however, is indeed a requirement. Whether you set the cookie manually, or rely upon the PHP session handler to do it for you. ;)

Link to comment
https://forums.phpfreaks.com/topic/272958-php-mysql-times/#findComment-1404919
Share on other sites

Seems like I wasn't specific enough in my opening line. What I meant to say was that I would not store the time in the cookie, as the user could easily defeat the system in that case.

 

Using a cookie to identify the user, however, is indeed a requirement. Whether you set the cookie manually, or rely upon the PHP session handler to do it for you. ;)

 

Then all is forgiven LOL :happy-04:

Link to comment
https://forums.phpfreaks.com/topic/272958-php-mysql-times/#findComment-1404938
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.