How to pass Session start time to javascript

Hello,
I wonder if you can help me?
I need to use the php session start time for timeout and timer functions by passing it onto an existing javascript code.

This is for an online test accessed after sucessful user log in.
I have copied the javascript which handles the timeout and coundown timer events.
What I need to know is if i can pass the $_session start time to the javascript and what would be the syntax for this?

Here is the script for the timer etc.
--------------------------------------------
<script type="text/javascript">
var minutes = 2;
var timeLeft = minutes*60*1000;
var tId = "";
var leave = "";
function countdown() {
       timeLeft -= 1000;

      if (timeLeft<=0){
      nextPage();
      } else {
      updateTimer();
   }
 }
window.onload=function() {
  //leave = setTimeout("nextPage()", timeLeft);
  tId = setInterval('countdown()',1000);
  updateTimer();

}
function updateTimer() {
   var timerArea = document.getElementById('timer');
   var numSeconds = timeLeft / 1000;
   var displaySeconds = numSeconds % 60;
   var displayMinutes = (numSeconds - displaySeconds) / 60;

   // format display a little more by putting a leading 0 for small seconds
   if (displaySeconds < 10) {
       displaySeconds = "0" + displaySeconds;
   }

      timerArea.value = displayMinutes + ":" + displaySeconds;
}
      function nextPage(){
            //assuming you have <form id="English1"...>
            var f = document.getElementById("English1");
            for( var i=0; i < f.elements.length; ++i)
            {
                    if( String(f.elements[i].type).toLowerCase() != "submit" )
                  f.elements[i].disabled=true;

            }
            clearInterval(tId);
            clearTimeout();
            alert('Time allowed for the English test has expired! Please press the Maths Test button to continue');
      }
       function activate(f){
  if (timeLeft > 100000 && confirm('Are you sure you want to leave the English test? You still have some time left to go back and check your answers. If you are sure you want to

go on to the Maths Test, please click OK button. Otherwise, click Cancel button to return to the English test and use the remaining minutes to improve your English score.'))
return false;
            for( var i=0; i < f.elements.length; ++i)
            {
                  f.elements[i].disabled=false;
            }
      return true;
      }
</script>
----------------------------
ErikalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Albert Van HalenAnalyst developerCommented:
You can store the session start time in a cookie, and access that cookie via javascript.
Or right after the session starts, yo can echo time() in a script tag. i.e. :
<script type="text/javascript">
sessionStartTime = '<? echo time(); ?>';

Perhaps this link is usefull to : http://devzone.zend.com/node/view/id/646
0
ErikalAuthor Commented:
Thank you.
How can I adapt my javascript code above, so that timeLeft variable is calculated from session start time and time() and checks is 15 minutes elapsed or not.
in other words: Supposing that time allowed for the test is 15 minutes,
can I use-
timeLeft = present_time() - sessionstart_time
 if (timeLeft<=0){
      nextPage();
      } else {
      updateTimer();
   }

and what would be the correct synta here for passing php var into javascript?
0
ErikalAuthor Commented:
Sorry correction:
timeLeft = (sessionstart_time + 15*minutes) - present time()
if (timeLeft<=0){
      nextPage();
      } else {
      updateTimer();
   }
0
Albert Van HalenAnalyst developerCommented:
The equivalent for php time in javascript is Math.round(new Date().getTime()/1000).
The difference after subtracting is in seconds.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ErikalAuthor Commented:
I am sorry, but I can not quite follow this.
I am a beginner at this.
OK I echo the time() in javascript, but how do I set the session start time in php? or do I need to?
One more problem. if the user leaves the page without submitting and logs out, and comes back a second time loging in again (hence starting a new session) how will the time limit set for the test work?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.