7 Replies - 7046 Views - Last Post: 26 June 2011 - 09:44 PM

#1 sonalmac  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 24-June 11

How to calculate "time passed" on a user's computer, throu

Posted 25 June 2011 - 04:27 PM

I am new-bee for PHP programming. I am trying to figure out time functions in PHP. However, I come to know these time and date functions work on the Server. I want to calculate what time a user takes to solve a problem. That means I need to know user's machine's timings. How can that be incorporated? Do I need to use Javascript?
Is This A Good Question/Topic? 0
  • +

Replies To: How to calculate "time passed" on a user's computer, throu

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2895
  • View blog
  • Posts: 10,028
  • Joined: 08-August 08

Re: How to calculate "time passed" on a user's computer, throu

Posted 25 June 2011 - 04:41 PM

Yes, use Javascript. PHP runs on the server. The user's computer never sees it.
Was This Post Helpful? 0
  • +
  • -

#3 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: How to calculate "time passed" on a user's computer, throu

Posted 25 June 2011 - 04:42 PM

Doing this in PHP isn't a good idea and better suited for Javascript.

For future reference...
The function time is used to get the current timestamp. Subtract the current timestamp from the old one to get the seconds elapsed.
<?php
$time = time();
sleep(3);
echo (time() - $time);
?>

Was This Post Helpful? 1
  • +
  • -

#4 sonalmac  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 24-June 11

Re: How to calculate "time passed" on a user's computer, throu

Posted 25 June 2011 - 11:40 PM

View PostCTphpnwb, on 25 June 2011 - 04:41 PM, said:

Yes, use Javascript. PHP runs on the server. The user's computer never sees it.



Thanks.. is there anything like endtime - starttime? Or I need to use a clock-style script? I am asking this because I don't know anything about JS. I was looking into date function, and it looks complicated.
Was This Post Helpful? 0
  • +
  • -

#5 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: How to calculate "time passed" on a user's computer, throu

Posted 26 June 2011 - 09:31 AM

You could use a cookie or session variable to set the time when they access the page, then once the submit the form or whatever it is you're planning on doing you could compare the current time against the time in their session variable. However, the timestamp could be faked so... yeah.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: How to calculate "time passed" on a user's computer, throu

Posted 26 June 2011 - 09:36 AM

Moved to Javascript
Was This Post Helpful? 0
  • +
  • -

#7 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: How to calculate "time passed" on a user's computer, throu

Posted 26 June 2011 - 11:40 AM

In Javascript you can use the Date.now() function. There are other ways to get the current time in Javascript. Check out the Date object

Example Using Date.now()
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var started = false;
var stime;
function timer()
{
	if(!started)
	{
		stime = Date.now();
		started = true;
		document.getElementById('button').innerHTML = 'Stop';
		document.getElementById('output').innerHTML = '';
	}
	else
	{
		document.getElementById('output').innerHTML = 'Time Taken: ' + ((Date.now() - stime) / 1000) + ' seconds';
		document.getElementById('button').innerHTML = 'Start';
		started = false;
	}
}
</script>
</head>
<body>
<div id="button" onclick="timer()">Start</div>
<div id="output"></div>
</body>
</html>

Was This Post Helpful? 1
  • +
  • -

#8 sonalmac  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 24-June 11

Re: How to calculate "time passed" on a user's computer, throu

Posted 26 June 2011 - 09:44 PM

View Postcodeprada, on 26 June 2011 - 11:40 AM, said:

In Javascript you can use the Date.now() function. There are other ways to get the current time in Javascript. Check out the Date object

Example Using Date.now()
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var started = false;
var stime;
function timer()
{
	if(!started)
	{
		stime = Date.now();
		started = true;
		document.getElementById('button').innerHTML = 'Stop';
		document.getElementById('output').innerHTML = '';
	}
	else
	{
		document.getElementById('output').innerHTML = 'Time Taken: ' + ((Date.now() - stime) / 1000) + ' seconds';
		document.getElementById('button').innerHTML = 'Start';
		started = false;
	}
}
</script>
</head>
<body>
<div id="button" onclick="timer()">Start</div>
<div id="output"></div>
</body>
</html>


Thanks a lot to explain with an example. I'll try to use it to solve my problem. Once again, thanks..

View Postgrimpirate, on 26 June 2011 - 09:31 AM, said:

You could use a cookie or session variable to set the time when they access the page, then once the submit the form or whatever it is you're planning on doing you could compare the current time against the time in their session variable. However, the timestamp could be faked so... yeah.


View Postgrimpirate, on 26 June 2011 - 09:31 AM, said:

You could use a cookie or session variable to set the time when they access the page, then once the submit the form or whatever it is you're planning on doing you could compare the current time against the time in their session variable. However, the timestamp could be faked so... yeah.


Thanks a lot. This is a neat idea, I can use PHP coding itself to work-out on user's timings.. thnanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1