Page 1 of 1

Calling a javascript function

Posted: Fri Oct 29, 2004 2:53 pm
by TNIDBMNG
I need to calculate the current time and I want the current time from the user's computer not my server so I'm using javascript to calculate the time. The original place I'm using the function I have a button that the user clicks to get the current time for start time. That all works fine but now I want to get the end time and update my database with it. Here is the function

Code: Select all

<script language="javascript">

function getEndTime()&#123;
	var now = new Date();
	var hours = (now.getHours()+17)-24;
	var minutes = now.getMinutes();
	var seconds = now.getSeconds()
	var endCall = "" + ((hours >12) ? hours -12 :hours)
	if (endCall == "0") timeValue = 12;
	endCall += ((minutes < 10) ? ":0" : ":") + minutes
	endCall += ((seconds < 10) ? ":0" : ":") + seconds
	endCall += (hours >= 12) ? " P.M." : " A.M."
	return endCall;
&#125;

</script>
For the end time I want to have a variable that the end time is assigned to. So my question is how do I call the javascript function and assign it to a variable. I tried $endTime = getEndTime(); but that didn't work.

Posted: Fri Oct 29, 2004 3:14 pm
by andre_c
the code will go from your server (PHP) to the browser (Javascript).
The result of getEndTime won'it go back to your server unless another request is sent.
One way to do it is to create a form with a hidden field and update the value of the hidden field with with the result of the getEndTime() and at the end of the getEndTime submit the form with javascript.
Good Luck.

Posted: Fri Oct 29, 2004 3:32 pm
by TNIDBMNG
so there is no way to assign the returned value to a variable?

Posted: Fri Oct 29, 2004 5:43 pm
by kettle_drum
As andre_c said..."[only if] another request is sent". So would place this time value in a form and send it back to the server. you can either make this process visable to the user - on the current page, or hidden - by sending the form in a hidden frame.

One other question, what are you trying to do? As im sure it could be solved without this process.

Posted: Fri Oct 29, 2004 5:47 pm
by TNIDBMNG
I am trying to get the current time. If I use the date() function in PHP it gives me the time of the server. I want to get the time of the user's computer (because in a lot of cases it will be different from our server).

Posted: Fri Oct 29, 2004 6:49 pm
by kettle_drum
In most cases when you want to get the users time, you ask them what time zone they are in and then work out their time with that detail. Then you get the correct time - as some users computer clocks maybe incorrect