Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Seeding a date jobject with php server time?

Status
Not open for further replies.

BobMCT

IS-IT--Management
Sep 11, 2000
756
US
I've seen this covered in other forums but for what ever reason I can't get it to work for me. Perhaps many of you have done this before and would be willing to advise?

I need to create a clock on a web page that starts with the calling server time. That means calling the js function with a parameter when constructing the web page.

My code looks something like this:

$SysTime = time();

print <<<EOD
<script>
function clock () {
var serverdate = new Date(" . $SysTime . "000");
}
function padlength(what) {
var output=(what.toString().length==1)? "0"+what : what
return output
}
function displaytime() {
serverdate.setSeconds(serverdate.getSeconds()+1)
var datestring = montharray[serverdate.getMonth()]+" "+padlength(serverdate.getDate())+", "+serverdate.getFullYear()
var timestring = padlength(serverdate.getHours()) + ":" + padlength(serverdate.getMinutes()) + ":" + padlength(serverdate.getSeconds())
document.getElementById("_clock").innerHTML = datestring+" " + timestring
}
</script>
EOD;

If I don't try to set the initial value of the date object it seems to work. But when I do it simply does nothing.

Thanks
 
This is more of a PHP question, however:

Your echo statement is very wrong, and will never produce a valid time for the javascript date function to use.

HEREDOC syntax like the one you are using does not allow the concatenation as you are attempting to do, since it treats everything inside as a string. So it literally outputs the SysTime variable value the quotes and the 3 0's into the Javascript date function which is of course invalid.

Try this:
Code:
$SysTime = time();
[red]$SysTime=$SysTime*1000;[/red]
print <<<EOD
<script>
function clock () {
  var serverdate = new Date([red]$SysTime[/red]);
}
function padlength(what) {
  var output=(what.toString().length==1)? "0"+what : what
  return output
}
function displaytime() {
  serverdate.setSeconds(serverdate.getSeconds()+1)
  var datestring = montharray[serverdate.getMonth()]+" "+padlength(serverdate.getDate())+", "+serverdate.getFullYear()
  var timestring = padlength(serverdate.getHours()) + ":" + padlength(serverdate.getMinutes()) + ":" + padlength(serverdate.getSeconds())
    document.getElementById("_clock").innerHTML = datestring+" " + timestring
}
</script>
EOD;

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top