I have been trying to show the server time on a website... this is the code I am using
<script type="text/javascript">
// Current Server Time script (SSI or PHP)- By JavaScriptKit.com (http://www.javascriptkit.com)
// For this and over 400+ free scripts, visit JavaScript Kit- http://www.javascriptkit.com/
// This notice must stay intact for use.
//Depending on whether your page supports SSI (.shtml) or PHP (.php), UNCOMMENT the line below your page supports and COMMENT the one it does not:
//Default is that SSI method is uncommented, and PHP is commented:
//var currenttime = '<!--#config timefmt="%B %d, %Y %H:%M:%S"--><!--#echo var="DATE_LOCAL" -->' //SSI method of getting server date
var currenttime = '<? print date("F d, Y H:i:s", time())?>' //PHP method of getting server date
///////////Stop editting here/////////////////////////////////
var montharray=new Array("Enero","Febrero","Marzo","Abril","Mayo","Ju nio","Julio","Agosto","Septiembre","Octubre","Novi embre","Diciembre")
var serverdate=new Date(currenttime)
function padlength(what){
var output=(what.toString().length==1)? "0"+what : what
return output
}
function displaytime(){
serverdate.setSeconds(serverdate.getSeconds()+1)
var datestring=montharray[serverdate.getMonth()]+" "+padlength(serverdate.getDate())+", "+serverdate.getFullYear()
var timestring=padlength(serverdate.getHours())+":"+pa dlength(serverdate.getMinutes())+":"+padlength(ser verdate.getSeconds())
document.getElementById("servertime").innerHTML=da testring+" "+timestring
}
window.onload=function(){
setInterval("displaytime()", 1000)
}
</script>
<span id="servertime"></span>
The problem I am having is that my server time shows something but the website shows an hour ahead. I have set my server time to AMERICA/El Salvador.
Can anybody help me? is there any other information you need from me? Thanks in advance
using server time on website
Moderator: General Moderators
Re: using server time on website
I'm not sure I understand what you are trying to accomplish but why can you just do:
Code: Select all
<span id="servertime"><?= date('Y-m-d H:i:s') ?></span>Re: using server time on website
cause with the script I am using the time is been updating live... it means that shows the seconds running.
The code you gave me is a static one... it just gives me the time when the page was generetad.
This is the URL where I have this script Click Here. The time is on the left side just below the weather update.
Thanks for your reply
The code you gave me is a static one... it just gives me the time when the page was generetad.
This is the URL where I have this script Click Here. The time is on the left side just below the weather update.
Thanks for your reply
Re: using server time on website
can anybody give me a hand with this? those anybody knows about this issue I am having?
Re: using server time on website
Does this work?
Code: Select all
<?php
//date_default_timezone_set('America/Montreal');
$timeStamp = date("F d, Y H:i:s");
?>
<html>
<head>
<script type="text/javascript">
var montharray = new Array("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Novi embre","Diciembre");
var temporalOffset = new Date - new Date('<?= $timeStamp ?>');
function displaytime() {
var currentDate = new Date();
var serverDate = new Date(currentDate.valueOf() + temporalOffset);
document.getElementById("servertime").innerHTML = serverDate.getHours() + ':' + serverDate.getMinutes() + ':' + serverDate.getSeconds();
}
window.onload = function() {
setInterval("displaytime()", 1000);
}
</script>
</head>
<body>
<span id="servertime"></span>
</body>
</html>
Re: using server time on website
now is showing an hour behind.marcth wrote:Does this work?
Re: using server time on website
You must realize that PHP is a server language; that is, it only executes in the server before the script is sent to the browser, so it can only deliver the time it was sent--static, as you said. You could update that every second or whatever you want, by using Ajax, but that will cause a heavy load on the server. I would settle for just using the local computer's time, shifted for time zone, in Javascript. Here's a complete guide to doing that: http://articles.techrepublic.com.com/51 ... 16329.html
In any case, if you were going to use PHP, you should not use the "short tag"-- <?. The opening tag for PHP should always be the full tag-- <?php.
In any case, if you were going to use PHP, you should not use the "short tag"-- <?. The opening tag for PHP should always be the full tag-- <?php.
Re: using server time on website
Probably I didnt make myself clear... the code WORKS but the time that is shows is one hour ahead (first code) or one hour below (second).califdon wrote:You must realize that PHP is a server language; that is, it only executes in the server before the script is sent to the browser, so it can only deliver the time it was sent--static, as you said. You could update that every second or whatever you want, by using Ajax, but that will cause a heavy load on the server. I would settle for just using the local computer's time, shifted for time zone, in Javascript. Here's a complete guide to doing that: http://articles.techrepublic.com.com/51 ... 16329.html
In any case, if you were going to use PHP, you should not use the "short tag"-- <?. The opening tag for PHP should always be the full tag-- <?php.