PDA

View Full Version : Date/Time/User


Stoked
04-07-2003, 01:59 AM
How do you make a php code that displays the Date and Time, and the clock to keep moving, like say I got on the site at 9:25 and ive been on the site for 5 mins so the time should say 9:30 without refreshing the page, so the time keeps counting.
Also I want to know how to put how many users are on the site at a time.

Thanx
Stoked

freddycodes
04-07-2003, 04:13 PM
As for the date and time, PHP cannot do that. PHP is a server-side language and without refreshing the page could only show the time once per page load. Javascript on the other hand can do it. Start there and look for how to do it with javascript. I'll give you a hint, you will need to use a div tag and use javascript to write the date and time to the div tag every second.

For the number of people online. You will need to use sessions in PHP and somehow count the number of active sessions. This will most likely mean storing session data in a database and checking for expired sessions every so often.

Stoked
04-07-2003, 08:19 PM
Thanx for the info. Could you happen to point me to a tutorial that can teach me the javascript and php code for these. Maybe a direct link if not too much trouble.
Thanx

freddycodes
04-07-2003, 09:08 PM
Here is the clock code.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script language="JavaScript1.1">
<!--
//Realtime Clock
//By: Louie Simpson
//http://www.irq11.com/~louie/
var domc = document.getElementById ? 1:0
var clockDiv;

function show_time()
{
if(domc)
{
clockDiv = document.getElementById("dateDiv");
}
else
{
clockDiv = document.dateDiv;
}
var d = new Date();
var h = (d.getHours() > 12) ? d.getHours() - 12 : d.getHours();
var l = (d.getHours() > 12) ? 'PM' : 'AM';
var m = (d.getMinutes() > 10) ? d.getMinutes() : "0" + d.getMinutes().toString();
var s = (d.getSeconds() > 10) ? d.getSeconds() : "0" + d.getSeconds().toString();
clockDiv.innerHTML = h + ":" + m + ":" + s + " " + l;
setTimeout('show_time()', 1000);
}
//-->
</script>
</head>

<body onLoad="show_time();">

My Clock<br />
<div id="dateDiv"></div>


</body>
</html>



Here is an article about sessions and users online
http://www.devarticles.com/art/1/215

Stoked
04-07-2003, 09:55 PM
That code worked but one more thing...Can I get it so where the clock reads my time for me and anyone else who looks at it? So if my clock says 4:54 PM and they live in England they see 4:54 PM.

freddycodes
04-07-2003, 10:59 PM
All client side applications use the local user's setting to display the date and time. So given that, only a server-side application can give the server time. In order to have a server time change every second as the user is on a page, the server would have to push the time out every second. Not a very efficient techinque given all you want to pass the new time.

Another option would be to convert the users local time to GMT and then find out England's time based on GMT, which I think is +1.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script language="JavaScript1.1">
<!--
//Realtime Clock
//By: Louie Simpson
//http://www.irq11.com/~louie/
var domc = document.getElementById ? 1:0
var clockDiv;

function show_time()
{
if(domc)
{
clockDiv = document.getElementById("dateDiv");
}
else
{
clockDiv = document.dateDiv;
}
var d = new Date();
var h = (d.getTimezoneOffset()/60) + d.getHours() + 1;
h = (h > 12) ? h - 12 : h;
var l = (d.getHours() > 12) ? 'PM' : 'AM';
var m = (d.getMinutes() > 10) ? d.getMinutes() : "0" + d.getMinutes().toString();
var s = (d.getSeconds() > 10) ? d.getSeconds() : "0" + d.getSeconds().toString();
clockDiv.innerHTML = h + ":" + m + ":" + s + " " + l;
setTimeout('show_time()', 1000);
}
//-->
</script>
</head>

<body onLoad="show_time();">

Time in England<br />
<div id="dateDiv"></div>


</body>
</html>

Stoked
04-08-2003, 10:07 PM
I think you missunderstood me or I type what i meant to say, but do you know how to set the time so it is set to eastern time always no matter what time zone the other person is in? Like if I lived in Pacific Time Zone and I looked at a website with the eastern time zone on it I should see the eastern time and not pacific.

freddycodes
04-09-2003, 12:33 AM
Thats what it does. It converts the users localtime to GMT time, then you just need to add or subtract the number of hours off of that based on your time zone? You get it?

Stoked
04-09-2003, 02:05 AM
Oh....I get it I was testing it wrong...I just changed my time by an hour and not the time zone(dumbass me). Sorry for that post. But thanx for the response.