Calling a javascript function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TNIDBMNG
Forum Newbie
Posts: 21
Joined: Wed Aug 18, 2004 12:22 pm

Calling a javascript function

Post 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.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post 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.
TNIDBMNG
Forum Newbie
Posts: 21
Joined: Wed Aug 18, 2004 12:22 pm

Post by TNIDBMNG »

so there is no way to assign the returned value to a variable?
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
TNIDBMNG
Forum Newbie
Posts: 21
Joined: Wed Aug 18, 2004 12:22 pm

Post 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).
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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
Post Reply