miércoles, 28 de noviembre de 2012

Reloj en PHP y JS para tu web

[php title="reloj.php"]
<script>
window.onload=hora;
var fecha = new Date('<?php echo date("d M Y G:i:s"); ?>');
function hora(){
var current=' a.m.'
var hora=fecha.getHours();
var minutos=fecha.getMinutes();
var segundos=fecha.getSeconds();
if(hora<10){ hora='0'+hora;};
if(minutos<10){minutos='0'+minutos; };
if(segundos<10){ segundos='0'+segundos; };
if(hora>12){ hora=hora-12; var current=' p.m.'};
fech=hora+':'+minutos+':'+segundos+current;
document.getElementById('hora').innerHTML=fech;
fecha.setSeconds(fecha.getSeconds()+1);
setTimeout('hora()',1000);
}
</script>
<div id='hora'>
</div>
[/php]

Implementación:
Al realizar el código anterior en el archivo reloj.php, se realiza un llamado a través de un include() en un archivo index.php, ejemplo:

[html highlight="7" title="index.php"]
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
text/html; charset=utf-8" />
<title>Reloj</title>
</head>
<?php include("reloj.php");?>
<body>
</body>
</html>
[/html]


download

No hay comentarios.:

Publicar un comentario