Jump to content

Recommended Posts

Posted

Vinha aqui pedir-vos ajuda sobre este código:

<?php $portugal_time = " ".date("G:i:s", strtotime(" - 1 hours")); echo "$portugal_time"; ?>

era o seguinte, gostaria de saber como faço para o relógio não parar, ou seja, os segundos estarem sempre a "rodar" e não apenas só quando fazemos refresh a página...

Posted
function hora() {
    var data = new Date();
    var sem = data.getDay();
    var dia = data.getDate();
    var mes = data.getMonth();
    var ano = data.getFullYear();    // Firefox apresenta bug ainda, mas basta substituir 'data.getYear();' pelo ano corrente
    var hora = data.getHours();
    var min = data.getMinutes();
    var seg = data.getSeconds();

    if (seg < 10) { seg = "0" + seg; }
    if (min < 10) { min = "0" + min; }
    m = new Array()
    m[0] = "Janeiro"
    m[1] = "Fevereiro"
    m[2] = "Março"
    m[3] = "Abril"
    m[4] = "Maio"
    m[5] = "Junho"
    m[6] = "Julho"
    m[7] = "Agosto"
    m[8] = "Setembro"
    m[9] = "Outubro"
    m[10] = "Novembro"
    m[11] = "Dezembro"
    d = new Array()
    d[0] = "Domingo"
    d[1] = "Segunda-feira"
    d[2] = "Terça-feira"
    d[3] = "Quarta-feira"
    d[4] = "Quinta-feira"
    d[5] = "Sexta-feira"
    d[6] = "Sábado"
   tempo.innerHTML = d[sem] + ", " + dia + " de " + m[mes] + " de " + ano + ", " + hora + ":" + min+ ":" + seg;
   setTimeout("Tempo()", "1000");
  }

<!--  ---- -->
<body onload="hora()">
<div id="tempo"> </div>
<!--  ---- -->

Estranha forma de vida que tem a capacidade de transformar comandos em mensagens de erro.

ndsotware.org

Posted

Já consegui. Se alguém quiser está aqui:

function showTime(){
 var time = new Date();
 var hour = time.getHours();
 var minute = time.getMinutes();
 var second = time.getSeconds();

 if(hour<10) hour = "0"+hour;
 if(minute<10) hour = "0"+minute;
 if(second<10) hour = "0"+second;
 var tempo = hour+":"+minute+":"+second;

 document.getElementById("timer").innerHTML=tempo;
}
function initTime(){
 setInterval(showTime, 1000);
}

<body onload="initTime();">
<span id="timer"></span>
Posted

@taviroquai, sim foi xdd

Sera que consegues colocar este script apenas com a hora de Portugal continental? :

function showTime(){
var time = new Date();
var hour = time.getHours();
var minute = time.getMinutes();
var second = time.getSeconds();


if(hour<=10) hour = "0"+hour;
if(minute<=10) minute = "0"+minute;
if(second<=10) second = "0"+second;
var tempo = hour+":"+minute+":"+second;

document.getElementById("timer").innerHTML=tempo;
}
function initTime(){
setInterval(showTime, 0);
}
Posted (edited)

No PHP :

<?php

# fuso horário
date_default_timezone_set('Europe/Lisbon');

$portugal_time = " ".date('G:i:s');

echo '<div id="time">'.$portugal_time.'</div>';

?>

Depois disto tens de usar o javascript para pegar no valor que o PHP imprimir na data e dar continuidade ao relógio. Aqui tens um exemplo : http://jsfiddle.net/yodapt/NFMX6/2/

Edited by yoda
Posted

@yoda, desculpa estar a ser chato mas eu não consegui fazer isso. Eu meto o php mas ao colocar o JS o relógio fica na mesma, não fica sempre a "rodar". Será que me podias ajudar? Desculpe mais uma vez pelo incomodo..

Posted (edited)

@shizy

Outra solução...



<?php
date_default_timezone_set('Europe/Lisbon');
$portugal_time = time()*1000;
?>
<span id="timer"></span>
<script type="text/javascript">
function showTime() {

   var hour = time.getHours();
   var minute = time.getMinutes();
   var second = time.getSeconds();

   if(hour<=10) hour = "0"+hour;
   if(minute<=10) minute = "0"+minute;
   if(second<=10) second = "0"+second;
   var tempo = hour+":"+minute+":"+second;

   document.getElementById("timer").innerHTML=tempo;
   time.setSeconds(time.getSeconds()+1);
}

var time = new Date(<?=$portugal_time?>);
var interval = setInterval(showTime, 1000);
</script>

EDIT: claro que estava a andar para trás lol... já corrigi nesta linha:

time.setSeconds(time.getSeconds() + 1 );
Edited by brunoais
Mesmo que tenh php, o main é o javascript

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.