SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Making a timer

  1. #1
    SitePoint Addict
    Join Date
    Oct 2005
    Location
    Home
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Making a timer

    I have a little game I made for adding numbers together. I created this game for elementary students at a school near me. Something to practice their skills with numbers.

    Here is the game basics:
    Choose 2 numbers in which numbers will be chosen from at random. A new page will load, asking an addition problem. Once the problem has been answered, they will directed to another page which will determine if the answer was correct or not. Once determined, they will be directed to another new problem, until finished with the game.

    Here is what I want:
    I don't know which language would be better, JavaScript or PHP, so that is why I posted this question in the "general" area. I want to have a timer count only when the problem is being asked. Then I want to be able to save the time in a variable, and add it as cumulative time for the end.

    If you need more clarification, please ask

    Thank you.

  2. #2
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With PHP, as far as I know, you would have to capture the timestamp at page load and then at the next page load (presumably the next question) and then take the difference between the two times as the time it took to answer the question. You can't keep a continuous timer going, as you could with Javascript.
    Nick . all that we see or seem, is but a dream within a dream
    Show someone you care, send them a virtual flower.
    Good deals on men's watches

  3. #3
    SitePoint Addict
    Join Date
    Oct 2005
    Location
    Home
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Possibility View Post
    With PHP, as far as I know, you would have to capture the timestamp at page load and then at the next page load (presumably the next question) and then take the difference between the two times as the time it took to answer the question. You can't keep a continuous timer going, as you could with Javascript.
    Which is exactly what I was going for. You hit the nail right on the head! I want to have a timer of how long they were on a certain page. Then add up the time at the end.

  4. #4
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well you could keep the current ongoing time in a session variable and the timestamp from the last page in another session variable, and then subtract that from the current timestamp and add that difference to the first session variable. Or you could do this with hidden form fields.

    So when a page loads, you save the timestamp: $_SESSION[starttime'] = Date("U").

    Then when the next page loads, you get the difference: $diff = Date("U") - $_SESSION['starttime'] and overwrite $_SESSION['starttime'] to reflect the new page load.

    And then add it to the ongoing sum: $_SESSION['totaltime'] += $diff

    You can use whatever time feature you'd like, I just chose the unix time stamp (look for "U" at the bottom of the table)

    Hope that is clear enough...
    Nick . all that we see or seem, is but a dream within a dream
    Show someone you care, send them a virtual flower.
    Good deals on men's watches

  5. #5
    SitePoint Addict
    Join Date
    Oct 2005
    Location
    Home
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Possibility View Post
    Well you could keep the current ongoing time in a session variable and the timestamp from the last page in another session variable, and then subtract that from the current timestamp and add that difference to the first session variable. Or you could do this with hidden form fields.

    So when a page loads, you save the timestamp: $_SESSION[starttime'] = Date("U").

    Then when the next page loads, you get the difference: $diff = Date("U") - $_SESSION['starttime'] and overwrite $_SESSION['starttime'] to reflect the new page load.

    And then add it to the ongoing sum: $_SESSION['totaltime'] += $diff

    You can use whatever time feature you'd like, I just chose the unix time stamp (look for "U" at the bottom of the table)

    Hope that is clear enough...
    It's decently clear, I will see what I can do with it tonight. Thank you.

  6. #6
    SitePoint Addict
    Join Date
    Oct 2005
    Location
    Home
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried mktime(); but I know that doesn't work for sure. Let me explain my situation a different way. I want to have a value that shows how long someone has been on one certain page.

  7. #7
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Date("U") is the number of seconds since the epoch. So if you take a capture of Date("U") when they load the first place, and subtract it from Date("U") when the next page is loaded, you'll know how many seconds they were on the first page - this, of course, does not take into consideration if a user loads the page and then leaves it up to return to later.
    Nick . all that we see or seem, is but a dream within a dream
    Show someone you care, send them a virtual flower.
    Good deals on men's watches

  8. #8
    Non-Member
    Join Date
    Nov 2008
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's very useful. It has helped me too.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •