using server time on website

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
guanaco
Forum Newbie
Posts: 5
Joined: Fri Aug 29, 2008 9:14 am

using server time on website

Post by guanaco »

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
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: using server time on website

Post by marcth »

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>
guanaco
Forum Newbie
Posts: 5
Joined: Fri Aug 29, 2008 9:14 am

Re: using server time on website

Post by guanaco »

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
guanaco
Forum Newbie
Posts: 5
Joined: Fri Aug 29, 2008 9:14 am

Re: using server time on website

Post by guanaco »

can anybody give me a hand with this? those anybody knows about this issue I am having?
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: using server time on website

Post by marcth »

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>
 
guanaco
Forum Newbie
Posts: 5
Joined: Fri Aug 29, 2008 9:14 am

Re: using server time on website

Post by guanaco »

marcth wrote:Does this work?
now is showing an hour behind. :(
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: using server time on website

Post by califdon »

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.
guanaco
Forum Newbie
Posts: 5
Joined: Fri Aug 29, 2008 9:14 am

Re: using server time on website

Post by guanaco »

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.
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).
Post Reply