time remainig

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • omerbutt
    Contributor
    • Nov 2006
    • 638

    time remainig

    hi
    i have an application for online test i have to show the time remainig and update it accordingly and show that how much time is left for the paper
    the admin gives the time limit in minutes
    say he gives 60 minutes for the paper now when the paper starts it should shw the remaining time ,i have been pulling my hairs from hours and could not get it done , i am convertinng the current time to seconds the take the allowed mionutes and convert them to seconds and then add it up into the resultant of the current time this way i can get the futer time limit when the test is over but i want to show the time remaining in minutes like 59mins left 58 mins left i just want any one to help me out
    Code:
    $time=date("h:i:s");
    $t=split(":",$time);
    $a=$t[0]*60;
    $st=$a+$t[1];
    $st=$st*60;
    $st=$st+$a[2];
    $given_time_seconds=60*60;
    $limit=$st+$given_time_seconds;
    $remaining_time=$limit-$st;
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you mean something like
    Code:
    <?php
    define('ALLOWED_TIME_MIN', 60);
    $a = new DateTime('2009-08-08 14:07:23'); // start time (needs to be set)
    $b = new DateTime(); // current time (variable)
    
    echo ALLOWED_TIME_MIN - $a->diff($b)->i; // time difference in minutes
    ?>

    Comment

    • omerbutt
      Contributor
      • Nov 2006
      • 638

      #3
      Originally posted by Dormilich
      you mean something like
      Code:
      <?php
      define('ALLOWED_TIME_MIN', 60);
      $a = new DateTime('2009-08-08 14:07:23'); // start time (needs to be set)
      $b = new DateTime(); // current time (variable)
      
      echo ALLOWED_TIME_MIN - $a->diff($b)->i; // time difference in minutes
      ?>
      i aint got any idea about how to do it cause havent done that before but i tried you method and it gave me an error it needs the class i think
      Fatal error: Call to undefined method DateTime::diff( ) in F:\xampp\htdocs \sfanProjects\e xamples\jqtime. php on line 85

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        looks like PHP 5.2. (got that same error with my built-in PHP).

        it works with PHP 5.3. (though DateTime is supported since 5.2, some methods require 5.3)

        if that’s not an option (upgrading) you probably need something along
        Code:
        $remain = ALLOWED_TIME_MIN * 60 - time() + time(strtotime('2009-08-08 14:07:23'));

        Comment

        • omerbutt
          Contributor
          • Nov 2006
          • 638

          #5
          how can i get the server time as it is an online test application and the user can change or reverse the time on his pc to extend the time limit

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            take the time when the test starts and save it in a session. if the user tries to break the session, disqualify him/her.

            changing the user machine’s time won’t work, since your script uses your server’s time.

            Comment

            • omerbutt
              Contributor
              • Nov 2006
              • 638

              #7
              means if he intentionally or un-intentionally presses the refresh button the time will not restart and continue from the begining....bec ause i think i was sitting on the same machine where the application was residing and when i changed the time on the clock it changed in the paper too , but i think you are right when the application will be installed at the client end then the user would be sitting on their PC and not the server where the application is residing and changing the time on the users machine wont effect the original PAper Time .............ap art from this .....about how the process flows
              i am doing the same thing when the page loads i take the given time say 20 mins and then add those minutes to the current time and mark the limit if lets say currently is 3:10 pm and he is allowed 20mis for the exam i add the minutes and the end time is 3:30 pm and save it into the session variable and then after every minute it makes an ajax calls that subtracts the current time from the end time and calculates the time left , is this alright means is there any thing or check i should be adding
              regards,
              Omer Aslam

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                Originally posted by omerbutt
                is this alright means is there any thing or check i should be adding
                there’s currently nothing coming to my mind. you only need to test thoroughly.

                Comment

                • omerbutt
                  Contributor
                  • Nov 2006
                  • 638

                  #9
                  ok
                  that's fine
                  regards,
                  Omer Aslam

                  Comment

                  Working...