0

As you see below I have code for a clock including the date that is set 2 weeks in advance, however it is not working either locally or on a server.

Could somebody please tell me what I am doing wrong?

The code is also below

<script type="text/javascript">
    tday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
    tmonth=new Array("January","February","March","April","May","June","July","August","September","October","November","December");

    function GetClock(){
        var d=new Date(+new Date + 12096e5);
        var dx=d.toGMTString();
        dx=dx.substr(0,dx.length -3);
        d.setTime(Date.parse(dx))
        d.setSeconds(d.getSeconds() + <?php     date_default_timezone_set('Europe/London'); echo date('Z'); ?>);
        var nday=d.getDay(),nmonth=d.getMonth(),ndate=d.getDate(),nyear=d.getYear(),nhour=d.getHours(),nmin=d.getMinutes(),nsec=d.getSeconds(),ap;

        if(nhour==0){ap=" AM";nhour=12;}
        else if(nhour<12){ap=" AM";}
        else if(nhour==12){ap=" PM";}
        else if(nhour>12){ap=" PM";nhour-=12;}

        if(nyear<1000) nyear+=1900;
        if(nmin<=9) nmin="0"+nmin;
        if(nsec<=9) nsec="0"+nsec;

        document.getElementById('clockbox').innerHTML=""+tday[nday]+", "+tmonth[nmonth]+" "+ndate+", "+nyear+" "+nhour+":"+nmin+":"+nsec+ap+"";
    }

    window.onload=function(){
        GetClock();
        setInterval(GetClock,1000);
    }
</script>
<div id="clockbox"></div>
gregg legg
  • 13
  • 4
  • You are giving yourself a hard time for nothing. Somebody else already figured out all this mess. Use Moment.js : http://momentjs.com/ – Jeremy Thille Feb 26 '15 at 16:07
  • Take a look here [How to put php inside javascript?](http://stackoverflow.com/questions/3345457/how-to-put-php-inside-javascript) – Alex Char Feb 26 '15 at 16:07
  • @JeremyThille . . . just because someone else has done it already, doesn't mean that there isn't value in trying to do see if you can do it yourself (and, maybe, even better ;) ) . . . – talemyn Feb 26 '15 at 16:14
  • Reactions to someone saying "Save up some time, it has already been done" are various. Some people reply "+1 for using something that's already been done - why bother?" and some other people say "doesn't mean that there isn't value in trying to do see if you can do it yourself?" I think both answers are valid. This was not my answer, just a comment, just an advice, just a hint. – Jeremy Thille Feb 26 '15 at 16:20

2 Answers2

5

Your page has the error:

 Uncaught SyntaxError: Unexpected token <

Because PHP

<?php     date_default_timezone_set('Europe/London'); echo date('Z'); ?> 

does not run inside a .html file. You have to change the extension to .php and your server has to support PHP.

And as @Oriol has pointed out, as an alternative, you could configure your server to process .html as PHP files.

PeterKA
  • 24,158
  • 5
  • 26
  • 48
  • 1
    Alternatively, the server could be configured to run php in .html files too. But better change the extension. – Oriol Feb 26 '15 at 16:11
0

To answer your question, it is not working because the server isn't processing your php code because it is a .html page.

Check here how to fix it:

How do I add PHP code/file to HTML(.html) files?

Community
  • 1
  • 1
Pinx0
  • 1,248
  • 17
  • 28