Jump to content
MakeWebGames

JS time


Zero-Affect

Recommended Posts

<span id="servertime"></span>

[js]<script type="text/javascript">

function show() {

var timda = '<php? date("F jS h:i:s A") ?>'

document.getElementById('servertime').innerHTML = timda;

setTimeout("show()",1000)

}

show()

</script>[/js]

I know im doing something wrong just can't picture it.

I am a complete newb when it comes to JS

Link to comment
Share on other sites

Well, javascript is run in the browser and php is run on the server. When the page loads, the php adds the current time to your var timda, and then you can consider it 'hardcoded' to the javascript.

All the function calls you do after that with javascript will load that exact same timestamp, meaning your div won't change at all. You're just reloading the same string into it over and over again.

If you really want to use php for the time, you can either use php to give the starting timestamp, and use javascript to increment the time, otherwise you'll need to use ajax to reload the time from php.

But there's not really any need for that, javascript has its own time functions. If you want to display time you can do it purely with javascript.

Link to comment
Share on other sites

Wow Zeggy thanks for that pal.

EDIT:

[js]

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin

var timerRunning = false;

var timezone = "Greenwich Mean Time"; // what time zone are you in ?

var adjust = 0;

function timeCheck(tzone, diff) {

if (timerRunning) {

clearTimeout(updatetime);

timerRunning = false; }

gmtOffset=eval(diff+adjust);

timezone = tzone;

checkDateTime();

}

function checkDateTime () {

var today = new Date();

var month = today.getMonth()+1;

var date = today.getDate();

var day = today.getDay();

var hour = today.getHours();

var minute = today.getMinutes();

var second = today.getSeconds();

var lastSat = date - (day+1);

while (lastSat < 32) lastSat+=7;

if (lastSat > 31) lastSat+=-7;

var firstSat = date - (day+1);

while (firstSat > 0) firstSat+=-7;

if (firstSat < 1) firstSat+=7;

if ((((month == 4) && (date >= firstSat)) || month > 4) &&

(month < 11 || ((month == 10) && day <= lastSat))) adjust += 60;

yourOffset = (new Date()).getTimezoneOffset();

yourOffset = yourOffset + adjust;

var xx = navigator.appName

var xy = navigator.appVersion;

xy = xy.substring(0,1);

if ((xy == 4) && (xx == "Netscape")) yourOffset = yourOffset+adjust;

if ((((month == 4) && (date > 20)) || month > 4) && (month < 11 || ((month == 10) &&

day < 30))) adjust -= 60;

ourDifference = eval(gmtOffset - yourOffset);

var half = eval(ourDifference % 60);

ourDifference = Math.round(ourDifference / 60);

hour = eval(hour - ourDifference);

if ((half == -30) || (half == 30)) minute += 30;

if (minute > 59) minute -= 60, hour++;

if (minute < 0) minute += 60, hour--;

if (hour > 23) hour -= 24, date += 1;

if (hour < 0) hour += 24, date --;

 

var dateTime = hour;

dateTime = ((dateTime < 10) ? "0":"") + dateTime;

dateTime = " " + dateTime;

dateTime += ((minute < 10) ? ":0" : ":") + minute;

dateTime += ((second < 10) ? ":0" : ":") + second;

dateTime += (hour >= 12) ? " PM" : " AM";

document.getElementById('servertime').innerHTML = dateTime;

updatetime=setTimeout("checkDateTime()", 900);

timerRunning = true;

}

// End -->

</SCRIPT>

[/js]

 

<BODY OnLoad="timeCheck(timezone, +360)">

 

<span id="servertime"></span>

 

Google'd it and came up with something like that

Link to comment
Share on other sites

Honestly? All that for something so simple? :P

Slap this in a file that display's nothing:

if(isset($_GET['time']))	{
echo date("F jS h:i:s A");
exit;
}

 

Now put this wherever you want to display your time:

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(function()	{
	setInterval("$('#servertime').load('url.php?time=true');",1000);
} );
</script>
<div id="servertime"></div>

 

Then just change the url to where it is going to be displayed in the JQuery section. :)

Link to comment
Share on other sites

I really didn't think about doing it with jQuery.

Here's what I got.

<span>This Server Time</span><p id="stime"></p>
<script>

var sdate = new Date('<?php echo date("F j, Y, g:i:s a"); ?>');
var seconds = sdate.getSeconds(),
minutes = sdate.getMinutes(),
hours = sdate.getHours();

function serverTime() {

seconds++;

if(seconds >= 60) {
	minutes++;
	seconds = 0;
}

if(minutes >= 60) {
	hours++;
	minutes = 0;
}
if(hours >= 12) {
	var sabr = 'PM';
} else {
	var sabr = 'AM';
}
//document.getElementById('stime').innerHTML='Time: '+sdate.getHours()+'.'+sdate.getMinutes()+'.'+sdate.getSeconds()+sabr;
document.getElementById('stime').innerHTML='Time: '+hours+'.'+minutes+'.'+seconds+' '+sabr; 
setTimeout('serverTime()', 1000);
}
serverTime();


</script>
Link to comment
Share on other sites

Using JQuery to query the server every second to get the time? Maybe it is short code but doesn't make it good/clever in any stretch of the imagination-do you get free bandwidth? Why make your server get hit like that? That is a real no imo. You can improve Djkanna's abit to let javascript do the logic (I assuming his works!) and keep time more accurately.

 

<span>This Server Time</span><p id="stime"></p>
<script> var sdate = new Date('<?php echo date("F j, Y, g:i:s a"); ?>');
var diff = sdate.getTime() - new Date().getTime();

function serverTime() {
sdate.setTime(new Date().getTime()+diff);
if(sdate.getHours() >= 12) {
	var sabr = 'PM';
} else {
	var sabr = 'AM';
}
document.getElementById('stime').innerHTML='Time: '+sdate.getHours()+'.'+sdate.getMinutes()+'.'+sdate.getSeconds()+sabr;
setTimeout('serverTime()',1000);
}
serverTime();
</script>

 

Edit:

(edit2: removed edit as it doesnt show 24hr)

Link to comment
Share on other sites

Using JQuery to query the server every second to get the time? Maybe it is short code but doesn't make it good/clever in any stretch of the imagination-do you get free bandwidth? Why make your server get hit like that? That is a real no imo.

Exactly, if all that's being done is display time, then the best solution is pure javascript. Apart from bandwidth issues, there's also problems with loading speed, disconnects, latency/inaccuracy, etc. Every browser should be capable of counting its own time :)

Link to comment
Share on other sites

Take a look again and see how more accurate mine is.

http://deadlykillah.net/folderhidden/test2.php

Hmm... let's see...

100 users online at the same time, 60 seconds per minute = 6,000 requests per second just for a simple timer.

It's a silly waste of bandwidth and processing power that could be used better elsewhere.

And it won't be more accurate than a javascript timer. There's always a latency delay and loading speed delay that you cannot avoid, while a javascript timer relies on the client's own cpu clock.

The javascript solutions posted above aren't ideal - they still require php to give the initial time. It would be more accurate to simply use javascript and grab the time straight from the browser. As it is right now, it's starting from the time that php parses that line, and there could be seconds between that event and when the javascript begins running on the client's browser.

Also the solutions use setTimeout. The time between each update then becomes 1000ms + whatever time it takes to execute timer update, and eventually the timer would be way out of sync with real time. It would be more accurate to use setInterval.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...