Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Time on Page - PHP

Options
  • 26-09-2007 2:44pm
    #1
    Registered Users Posts: 3,401 ✭✭✭


    Hi,

    I am making an online exam page and I was wondering how would you calculate the time it takes from when a page loads to when the submit button is clicked?


    Thanks
    Gary


Comments

  • Registered Users Posts: 3,594 ✭✭✭forbairt


    time when user opened page ... = startTime
    time when user submits page ... = endTime

    endTime - startTime = total time on page ...

    :D

    can you please for the love of god give more details on what you want ? as what I've just said above is a valid answer to your question

    what are your options ... what are your languages ... sessions allowed ... javascript ... and so on ?


  • Registered Users Posts: 3,401 ✭✭✭randombar


    All right, apologies!

    Basically page loads, they look at question, answer it and click submit.

    Like ya said:

    time when user opened page ... = startTime
    time when user submits page ... = endTime

    endTime - startTime = total time on page

    Languages, php and javascript (but I'm still crap at java)

    using sessions anyways

    I guess I've no idea how to even start this one?


  • Closed Accounts Posts: 7 AlbertH


    Easiest way is to use the time() function in PHP and then set a variable with it when the page is displayed.

    // Set the start time and put it as a variable in the form to be submitted.
    $starttime = time();

    <form....>
    <input type="hidden" name="starttime" value="<?php echo $starttime;?>" >


    When the form is submitted again use PHP to get the time

    //Get the endtime and read the starttime variable from the form.

    $endtime= time();
    $startime = $_POST["'starttime"];

    // This will give you the time in seconds - normal maths apply to work out minutes and hours...
    $timetaken = $endtime - $startime;


    I hope this helps.
    Albert.


  • Registered Users Posts: 3,594 ✭✭✭forbairt


    assuming you're keeping track of the tests in the system / database
    you shouldn't need to include the startTime ... have a timestamp in your test table in your database

    this should be updated when you create a new test ...

    then as Albert said ... find the end time ... and hey presto you've got your time difference :)


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    AlbertH wrote:
    Easiest way is to use the time() function in PHP and then set a variable with it when the page is displayed.

    // Set the start time and put it as a variable in the form to be submitted.
    $starttime = time();

    <form....>
    <input type="hidden" name="starttime" value="<?php echo $starttime;?>" >


    When the form is submitted again use PHP to get the time

    //Get the endtime and read the starttime variable from the form.

    $endtime= time();
    $startime = $_POST["'starttime"];

    // This will give you the time in seconds - normal maths apply to work out minutes and hours...
    $timetaken = $endtime - $startime;


    I hope this helps.
    Albert.
    The only issue here is injection.

    If the application is using the timestamps for any kind of reliable time measurement, then you can't rely on anything the browser sends.

    Best to use a session, store the start time in the session and then use that to calculate the time taken.

    There are five million different "right ways" to do this, and Albert's, forbairt's and mine are three of them. It all really depends on what the page is supposed to do though.


  • Advertisement
Advertisement