Marrying JavaScript to PHP

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!

Moderator: General Moderators

Post Reply
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Marrying JavaScript to PHP

Post by irishmike2004 »

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.

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>
';
}
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:

Code: Select all

echo '
'.$myDate=new Date().'
var date ="'.$currDate.'"
'.$hours=$myDate.getHours().'
'.$minutes=$myDate.getMinutes().'
'.$seconds=$myDate.getSeconds().'
var dn="'.$dn.'"
';
I am a bit reluctant to break the code without knowing if it will work or not :-)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

It looks like you're trying to just mix up javascript and php as a combined language. It can't be done that way but you can combine the two if you think about what the output of the php will do that javscript.

So...

Code: Select all

<script language="javascript">
<!-- Hide
var demo="<?php $demovar = "test";
    echo $demovar; ?>"

document.write(demo)
// End -->
</script>
would make a JS that looks like

Code: Select all

&lt;script language="javascript"&gt;
&lt;!-- Hide
var demo="test"

document.write(demo)
// End --&gt;
&lt;/script&gt;
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Sorry I think I replied to your post in too much of a hurry there. I'll look again... closer ;-)

You could just make a copy of the php document and test it then if it doesn't work just copy the original php document back into place.
Post Reply