Shoutbox

JavaScript Localtime - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: JavaScript Localtime (/showthread.php?tid=73515)

JavaScript Localtime by Eddie on 04-11-2007 at 01:42 PM

ok i did  a google search, and found something but it didnt work, and you all seem to be php whizzes so...

im looking for some php that posts the users local time onto my website, instead of the databases local time :) if thats possible in php ty :)

EDIT: Title Changed to JavaScript Local Time, Read my latest post :)


RE: PHP Localtime by Ezra on 04-11-2007 at 01:49 PM

You don't know the users local time unless they tell you and you store it in a database.

EDIT: It's possible however with Javascript.


RE: PHP Localtime by Eddie on 04-11-2007 at 01:51 PM

quote:
Originally posted by Ezra
You don't know the users local time unless they tell you and you store it in a database.
A PHP Script i found did it via javascript / php it just failed :(

http://www.mtdev.com/2002/07/display-local-time/
RE: PHP Localtime by absorbation on 04-11-2007 at 01:55 PM

You can't figure it out. PHP's time function is got from the server, not a browser, therefore the time(); function matches what the server's time is.

If you don't want to use timestamps, use gmtime(); which gives the correct GMT time (instead of the server's default time) :).

Edit: You could use javascript, although it can be difficult and it does contain a small amount of limitations.


RE: PHP Localtime by markee on 04-11-2007 at 02:07 PM

The reason why PHP cannot do this is because it is a server-side script, meaning it is all done from the server.  Javascript is a client side script and that is why it is very easy to do using javascript.  If you want some help, just give us the exact details of what you are after and we will head you in the right direction ;)


RE: PHP Localtime by Eddie on 04-11-2007 at 02:23 PM

quote:
Originally posted by markee
The reason why PHP cannot do this is because it is a server-side script, meaning it is all done from the server.  Javascript is a client side script and that is why it is very easy to do using javascript.  If you want some help, just give us the exact details of what you are after and we will head you in the right direction ;)
k...

Example of wot i want: Wednesday April 11, 2007, 10:22pm thats how i want it layed out :)
RE: PHP Localtime by Ezra on 04-11-2007 at 02:24 PM

But what do you want to show?

Just a clock function or something with database functions and or  forum posts behind it?


RE: JavaScript Localtime by Eddie on 04-11-2007 at 02:25 PM

Just a clock :)


RE: JavaScript Localtime by Ezra on 04-11-2007 at 02:36 PM

That's easy to find with a quick Google Search

EDIT: First result gave this:

code:
<script language="JavaScript">

<!--
// please keep these lines on when you copy the source
// made by: Nicolas - http://www.javascript-page.com

var clockID = 0;

function UpdateClock() {
   if(clockID) {
      clearTimeout(clockID);
      clockID  = 0;
   }

   var tDate = new Date();

   document.theClock.theTime.value = ""
                                   + tDate.getHours() + ":"
                                   + tDate.getMinutes() + ":"
                                   + tDate.getSeconds();
   
   clockID = setTimeout("UpdateClock()", 1000);
}
function StartClock() {
   clockID = setTimeout("UpdateClock()", 500);
}

function KillClock() {
   if(clockID) {
      clearTimeout(clockID);
      clockID  = 0;
   }
}

//-->

</script>
<body onload="StartClock()" onunload="KillClock()">
<center><form name="theClock">
<input type=text name="theTime" size=8>
<form></center>

RE: JavaScript Localtime by Eddie on 04-11-2007 at 02:39 PM

ty ezra, but can i have it so its not in a form please :)


RE: JavaScript Localtime by Ezra on 04-11-2007 at 02:48 PM

The example was more of something that you could adapt to your website not just copy/paste :P

Because it also uses the direct name of that form, it's better to use getElementById.

So just make a div you want to use for the clock, give it the ID of Clock or something and change that line "ocument.theClock.theTime.value = ...." to something like " document.getElementById("Clock").innerHTML = ..."

Also change the lines after that to make it have the same format as you wanted.


RE: JavaScript Localtime by Eddie on 04-11-2007 at 02:50 PM

kty ;)


RE: JavaScript Localtime by markee on 04-11-2007 at 02:59 PM

I think this is what you are after, I wrote it myself so it might be able to be made smaller (using functions that I don't know about) but it will do.  Just put it in wherever you are wanting the time to be displayed.

code:
<script language="javascript" type="text/javascript" >var d=new Date();var days=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");var months=new Array("January","February","March","April","May","June","July","August","September","October","November","December");document.write(days[d.getDay()]+" "+months[d.getMonth()]+" "+d.getDate()+", "+(d.getYear()+1900)+", "+(d.getHours()%12==0?"12":d.getHours()%12)+":"+(d.getMinutes()>9?d.getMinutes():"0"+d.getMinutes())+(d.getHours()<12?"am":"pm"));</script>


EDIT: should have refreshed the page I guess... and mine is only static, not a dynamic one like that either (it will only show the one time and not continually update... I guess you wanted that though)

EDIT2: fixed my code to have a prefixing 0 on the minutes when they are less than 10 :tongue:

EDIT3: Had a problem with the year that is now fixed.... why do they start at 1900?
RE: JavaScript Localtime by Eddie on 04-11-2007 at 03:33 PM

Thanks Markee :) Appreciated buddy :)


RE: JavaScript Localtime by Matti on 04-14-2007 at 02:05 PM

quote:
Originally posted by markee
EDIT3: Had a problem with the year that is now fixed.... why do they start at 1900?
Why don't you simply use d.getFullYear()? Much easier! :D