Jump to content

Recommended Posts

Posted

I have my Javascript, which is outputting 'NaN'. I don't want it to, and I want it to display the difference between their computer, and the server.

 

<html><head><title>Time</title>
<script type="text/javascript">function d() 
{var j=[<?php echo idate("H");?>,<?php echo idate("i");?>,<?php echo idate("s");?>];
var t=new Date();
var c=[t.getHours,t.getMinutes,t.getSeconds];
var a=[c[0]-j[0],c[1]-j[1],c[2]-j[2]];
var b="After Ours";
if (a[0]<0)
{a[0]=j[0]-c[0]; b="Before Ours";}
if (a[1]<0)
a[1]=j[1]-c[1];
if (a[2]<0)
a[2]=j[2]-c[2];
for (i=0;i<3;i++) 
document.getElementById("r"+i).innerHTML=a[0];
document.getElementById("r").innerHTML=b;
document.getElementById("t").innerHTML="Your computer's date and time:"+Date()+"<br>Our date and time:<?php 
$aop="AM";
if (idate('H')>12)
$aop="PM";
$dow=array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
$mow[1]='January';
$mow[2]='February';
$mow[3]='March';
$mow[4]='April';
$mow[5]='May';
$mow[6]='June';
$mow[7]='July';
$mow[8]='August';
$mow[9]='September';
$mow[10]='October';
$mow[11]='November';
$mow[12]='December';
$min=idate("i");
$sec=idate("s");
if ($min<10)
$min="0".idate("i");
if ($sec<10)
$sec="0".idate("s");
$d=array($dow[idate('w')],idate('d'),$mow[idate('m')],idate('Y'),idate('h'),$min,$sec,microtime(get_as_float),$aop);
echo $d[0]." ".$d[1]." ".$d[2]." ".$d[3]." ".$d[4].":".$d[5].":".$d[6].".".$d[7]." ".$d[8];
?>";}</script>
</head><body onload="d()"><div id="t"></div><br><div id="r0"></div>h <div id="r1"></div>m <div id="r2"></div>s <div id="r"></div>
<button onclick="window.location.href='servertime.php'">Update Time</button>
</body></html>

Posted

I want it to have the difference in time on there so that they can submit a form on to the proper time.

Posted
So how would I get the numbers on there instead on NaN. Why isn't it a number?

 

JavaScript has unlike Java no type assigned to a VAR.

The type can be changed when you assign a value.

To make sure you get a number and not a string you could force typecast the VAR to an integer (parseInt).

Posted

What I was trying to say is that your trying to execute the php through the javascript. You can't execute php through the client. Or either your javascript is wrong. I've never really learned javascript so I can't help you there.

 

 

Posted

I still say that your problem is your trying to execute php through a javascript function and it won't work. Quoted from the this thread:

 

http://www.codingforums.com/archive/index.php/t-2600.html

 

To understand why your approach is not working, it's really important to remember that JavaScript runs on the client and PHP on the server. As Spookster has already said above, when PHP is done with processing a file, it sends the output as a HTTP response back to the client. So generally, these two technologies can't be interoperable, since they don't run in the same environment.

 

Posted

Will you please post a link to your page so I can figure out what you want?

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...