简体   繁体   中英

JavaScript live clock is not running

php splits variable problem while passing in javascript or there is problem with the logic of runing live clock , any help will be appreciated, guide me where am i wrong ?

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">

function show(){
    var hours= '<?php echo $splits[0];?>';
    var minutes= '<?php echo $splits[1];?>';
    var seconds= '<?php echo $splits[2];?>';
    var dn="AM" 
    if (hours>12){
        dn="PM"
        hours=hours-12
        //this is so the hours written out is in 12-hour format, instead of the default //24-hour format.
    }
    if (hours==0)
        hours=12
    //this is so the hours written out when hours=0 (meaning 12a.m) is 12
    if (minutes<=9)
        minutes="0"+minutes
    if (seconds<=9)
        seconds="0"+seconds
    document.getElementById("demo").innerHTML=
    hours+":"+minutes+":"+seconds+" "+dn
    setTimeout("show()",1000);
}
</script>
</head>
<body onload="show()">
<?php
$date = new DateTime("now", new DateTimeZone('Asia/Kolkata') );
$duration = $date->format("H:i:s");
$splits =  explode(":",$duration);
settype($splits[0], "integer");
settype($splits[1], "integer");
settype($splits[2], "integer"); 
?>
<p id="demo"></p>
<h1 id="demo1"></h1>
</body>
</html>

In this case you can use vanilla JavaScript:

 <!DOCTYPE html> <html> <head> <script type="text/javascript"> function show(){ var timer = new Date(). toLocaleString('en-US', { timeZone: 'Asia/Kolkata' }) .split(',')[1] .trim(); document.getElementById("demo").innerHTML = timer setTimeout("show()",1000); } </script> </head> <body onload="show()"> <p id="demo"></p> </body> </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM