How can i create a live clock with php that gets time from server not users pc time [not javascript]
i used the below code but time stops when using php variable
<form name="Tick">
<input type="text" size="12" name="Clock">
</form>
<script type="text/javascript">
function show(){
var hours="<?php echo $myhour; ?>"
var minutes="<?php echo $mymin; ?>"
var seconds="<?php echo $mysec; ?>"
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.Tick.Clock.value=
hours+":"+minutes+":"+seconds+" "+dn
setTimeout("show()",1000)
}
show()
</script>
You can use ajax.
timestamp.php
<?php
date_default_timezone_set('YOUR TIMEZONE');
echo $timestamp = date('H:i:s');
jQuery
$(document).ready(function() {
setInterval(timestamp, 1000);
});
function timestamp() {
$.ajax({
url: 'http://localhost/timestamp.php',
success: function(data) {
$('#timestamp').html(data);
},
});
}
HTML
<div id="timestamp"></div>
PHP is a server-side programming language, Javascript is a client-side programming language.
The PHP code that fills the variables will only update when the webpage is loaded, after that you are left with Javascript code and nothing more.
I recommend you to search a basic programming book which mentions concepts such as client-side and server-side code because (and not trying to be harsh) you seems to have a big misunderstanding about how those things works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With