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>
4
  • You are giving yourself a hard time for nothing. Somebody else already figured out all this mess. Use Moment.js : momentjs.com Commented Feb 26, 2015 at 16:07
  • Take a look here How to put php inside javascript? Commented Feb 26, 2015 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 ;) ) . . . Commented Feb 26, 2015 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. Commented Feb 26, 2015 at 16:20

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Alternatively, the server could be configured to run php in .html files too. But better change the extension.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.