njaohnt Posted September 15, 2011 Share Posted September 15, 2011 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> Quote Link to comment Share on other sites More sharing options...
PenTester Posted September 16, 2011 Share Posted September 16, 2011 For me it showing(when i use xampp) h m s Update Time[button] Quote Link to comment Share on other sites More sharing options...
njaohnt Posted September 16, 2011 Author Share Posted September 16, 2011 So how would I get the numbers on there instead on NaN. Why isn't it a number? Quote Link to comment Share on other sites More sharing options...
Guest xaav Posted September 17, 2011 Share Posted September 17, 2011 Could you post an example page? Quote Link to comment Share on other sites More sharing options...
PenTester Posted September 17, 2011 Share Posted September 17, 2011 Actually what do you want to do? and why do you want to do? Quote Link to comment Share on other sites More sharing options...
njaohnt Posted September 17, 2011 Author Share Posted September 17, 2011 I want it to have the difference in time on there so that they can submit a form on to the proper time. Quote Link to comment Share on other sites More sharing options...
Guest xaav Posted September 17, 2011 Share Posted September 17, 2011 Could you post an example page? Quote Link to comment Share on other sites More sharing options...
Tjoene Posted September 18, 2011 Share Posted September 18, 2011 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). Quote Link to comment Share on other sites More sharing options...
Byron Posted September 18, 2011 Share Posted September 18, 2011 I'm like xaav, "can you post an example"? Where did you get this script? I'm thinking (and I could be wrong) the problem is that your trying to execute php through the browser when it's a server side language. Javascript is browser side. Your php is working correctly when it's ONLY php on a page: http://byrondallas.heliohost.org/temp/java-scrip-php.php Quote Link to comment Share on other sites More sharing options...
njaohnt Posted September 18, 2011 Author Share Posted September 18, 2011 Yes, it is on a php page. How do I change it's type Tjoene? Quote Link to comment Share on other sites More sharing options...
Byron Posted September 18, 2011 Share Posted September 18, 2011 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. Quote Link to comment Share on other sites More sharing options...
Wizard Posted September 18, 2011 Share Posted September 18, 2011 {var j=[<?php echo idate("H");?>,<?php echo idate("i");?>,<?php echo idate("s");?>]; Does this return an array of strings or an array of numbers? Also, rewrite your code to follow good practices: http://javascript.crockford.com/style1.html Quote Link to comment Share on other sites More sharing options...
njaohnt Posted September 18, 2011 Author Share Posted September 18, 2011 Numbers. The source of the page has unquoted numbers. Quote Link to comment Share on other sites More sharing options...
Byron Posted September 19, 2011 Share Posted September 19, 2011 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. Quote Link to comment Share on other sites More sharing options...
Guest xaav Posted September 19, 2011 Share Posted September 19, 2011 Will you please post a link to your page so I can figure out what you want? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.