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!
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
<script language="javascript">
function getEndTime(){
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;
}
</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.
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.
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.
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).
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