JavaScript 实时时钟不是 运行
JavaScript live clock is not running
php传入javascript时拆分变量问题或运行实时时钟的逻辑有问题,任何帮助将不胜感激,指导我哪里错了?
<!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>
在这种情况下,您可以使用原版 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>
php传入javascript时拆分变量问题或运行实时时钟的逻辑有问题,任何帮助将不胜感激,指导我哪里错了?
<!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>
在这种情况下,您可以使用原版 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>