Marrying JavaScript to PHP
Posted: Fri Dec 10, 2004 9:53 am
Hello:
I am working on a real-time clock for a website, the following code works but I wanted to replace the JS date functionality completely with PHP.
The problem I ran into was that the "real-time" update of the seconds didn't work because I was unsure of how to translate the PHP date() function to a model like the JS date() function which allows me to dynamically go get the date. I suppose the question is: "Will PHP do the same thing that the JS will if we modify the code to be:
I am a bit reluctant to break the code without knowing if it will work or not 
I am working on a real-time clock for a website, the following code works but I wanted to replace the JS date functionality completely with PHP.
Code: Select all
function pageClock()
{
// set clocks size and color
$tcolor = '#009999';
$tsize = '3';
// body tags may be removed if called in a PHP page that already echoes HTML
echo '
<body>
<div align="center">
<font size="'.$tsize.'" color="'.$tcolor.'"><b>
<span id="clock">
<SCRIPT LANGUAGE="JavaScript">
<!--
function getTheDate(){
';
// PHP Date function
$currDate = date("l, F j, Y");
$dn = date("A");
// JS Date string makes page update in real-time
echo '
var myDate=new Date()
var date ="'.$currDate.'"
var hours=myDate.getHours()
var minutes=myDate.getMinutes()
var seconds=myDate.getSeconds()
var dn="'.$dn.'"
';
//put the display together
echo'
if (hours==0)
hours=12
if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds
var cdate=date+" | "+hours+":"+minutes+":"+seconds+" "+dn
';
echo '
if (document.all)
document.all.clock.innerHTML=cdate
else if (document.getElementById)
document.getElementById("clock").innerHTML=cdate
else
document.write(cdate)
}
if (!document.all&&!document.getElementById)
getTheDate()
function goforit(){
if (document.all||document.getElementById)
setInterval("getTheDate()",1000)
}
window.onload=goforit()
// End -->
</script>
</span>
</b>
</font>
</div>
</body>
';
}Code: Select all
echo '
'.$myDate=new Date().'
var date ="'.$currDate.'"
'.$hours=$myDate.getHours().'
'.$minutes=$myDate.getMinutes().'
'.$seconds=$myDate.getSeconds().'
var dn="'.$dn.'"
';