Page 1 of 1

Would like to do this w/o JS

Posted: Mon Apr 04, 2005 8:18 am
by irishmike2004
Hi All:

This script:

Code: Select all

<body>
<div align=&quote;center&quote;>
<font size=&quote;3&quote; color=&quote;#009999&quote;><b>
<span id=&quote;clock&quote;>

<SCRIPT LANGUAGE=&quote;JavaScript&quote;>
<!--
function getTheDate(){

var dayArray=new Array(&quote;Sunday&quote;,&quote;Monday&quote;,&quote;Tuesday&quote;,&quote;Wednesday&quote;,&quote;Thursday&quote;,&quote;Friday&quote;,&quote;Saturday&quote;)
var monthArray=new Array(&quote;January&quote;,&quote;February&quote;,&quote;March&quote;,&quote;April&quote;,&quote;May&quote;,&quote;June&quote;,&quote;July&quote;,&quote;August&quote;,&quote;September&quote;,&quote;October&quote;,&quote;November&quote;,&quote;December&quote;)
var myDate=new Date()
var year=myDate.getYear()
if (year < 1000)
year+=1900
var day=myDate.getDay()
var month=myDate.getMonth()
var dayNum=myDate.getDate()
if (dayNum<10)
dayNum=&quote;0&quote;+dayNum
var year=myDate.getYear()
if (year < 1000)
year+=1900
var day=myDate.getDay()
var month=myDate.getMonth()
var dayNum=myDate.getDate()
var hours=myDate.getHours()
var minutes=myDate.getMinutes()
var seconds=myDate.getSeconds()
var dn=&quote;AM&quote;
var tzone=&quote;Central Daylight Time&quote;

if (hours==0)
hours=12
if (minutes<=9)
minutes=&quote;0&quote;+minutes
if (seconds<=9)
seconds=&quote;0&quote;+seconds

var cdate=dayArray&#1111;day]+&quote;, &quote;+monthArray&#1111;month]+&quote; &quote;+dayNum+&quote;, &quote;+year+&quote; | &quote;+hours+&quote;:&quote;+minutes+&quote;:&quote;+seconds+&quote; &quote;+dn+&quote; (&quote;+tzone+&quote;)&quote;


if (document.all)
document.all.clock.innerHTML=cdate
else if (document.getElementById)
document.getElementById(&quote;clock&quote;).innerHTML=cdate
else
document.write(cdate)
}
if (!document.all&&!document.getElementById)
getTheDate()

function goforit(){
if (document.all||document.getElementById)
setInterval(&quote;getTheDate()&quote;,1000)
}
window.onload=goforit()
//  End -->
</script>

</span>
</b>
</font>
</div>

</body>
has two PHP codes in the source, they set timezone and daypart (am/pm) the rest is handled by JavaScript. This is a dynamic clock (meaning it actually runs on the page)... I have been unable to get this to happen using PHP but would like to. The question is: "How do we do something this dynamic?"

The JS works fine, but with only two points where PHP code is inserted, I figure it might as well be completely JS or I need to find a way to do this same effect in PHP proper.

Thanks for any suggestions.

Posted: Mon Apr 04, 2005 8:30 am
by n00b Saibot
doing completely in JS : might not be implemented in some cases, but worthwhile to do.
doing completely in PHP: incurs a lot of server overhead, but will be always implemented. but not a good choice for such thing imo.

Posted: Mon Apr 04, 2005 9:14 am
by CoderGoblin
My opinion .. The question you need to ask is how important is this clock ? If the answer is "It's nice but not critical" then keep to javascript with a <noscript> tag to degrade nicely (with possibly message saying "clock disabled with javascript inactive"). If the clock is a critical part of the application you then have to consider what information needs to be transferred to keep it current. Not only would this result in extra traffic/bandwidth which you may have to pay for, it could be annoying to users to always be loading information, especially for modem users.

The best solution in this case is serve as much information from PHP initially then let JS take over but show information for the people who have javascript switched off.