2012-04-15 7 views
0

Ich habe ein Skript, das das Datum und die Uhrzeit anzeigt.Wie PHP Datum und Uhrzeit automatisch aktualisiert werden

Wie kann ich etwas hinzufügen, damit es automatisch aktualisiert wird, anstatt auf der Seite zu aktualisieren. Hier

ist das Skript:

<?php 
$hourdiff = 0; // Replace the 0 with your timezone difference (; 
$site = date("l, d F Y g:i a",time() + ($hourdiff * 3600)); 
echo $site; 
?> 

kann jemand helfen. Vielen Dank.

Antwort

5

PHP ist eine serverseitige Sprache, und nur die Art und Weise, wie Sie eine Anfrage an den Server senden, erfolgt durch Umleitung, Navigation, Aktualisierung oder durch eine Ajax-Anfrage.

Also, Sie brauchen Javascript dafür.

Hier ist ein einfaches demo

setInterval(function() { 
    var currentTime = new Date ();  
    var currentHours = currentTime.getHours (); 
    var currentMinutes = currentTime.getMinutes (); 
    var currentSeconds = currentTime.getSeconds (); 
    currentMinutes = (currentMinutes < 10 ? "0" : "") + currentMinutes; 
    currentSeconds = (currentSeconds < 10 ? "0" : "") + currentSeconds;  
    var timeOfDay = (currentHours < 12) ? "AM" : "PM";  
    currentHours = (currentHours > 12) ? currentHours - 12 : currentHours;  
    currentHours = (currentHours == 0) ? 12 : currentHours;  
    var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay; 
    document.getElementById("timer").innerHTML = currentTimeString; 
}, 1000); 
+1

thank you so much :) –