SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 29
  1. #1
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    user's time attached to same-site links?

    hiyer
    never done any javascript. only code stuff i know at all is php.

    in a serverside php script that i'm doing, i need to get the user's time somehow - what it is on their computer, in their country for them now.

    obviously a client side thing is needed.

    so what i'm looking for is a piece of javascript that'll get their time and attach it to the url so i can GET that time when (if) they go to another page, by looking at the end of that url

    not really sure how to do the javascript side of it

    the way i'm thinking is somehow attaching the time to every same site link somehow? is that possible/feasable? am i thinking along the right sort of lines?

    any pointers or ideas really 'preciated (bear in mind - zero javascript knowledge )
    thanks

  2. #2
    SitePoint Zealot jonsteele's Avatar
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can ge the time by using the following:

    <script>
    now = new Date();
    timeToAttach = now.getTime(); //format: milliseconds since unix epoch (not equivalent to time() in PHP)
    </script>

    Place that in the <head> of your page. Now you can access the timeToAttach variable anywhere. To append it to URL's, add the following onlick handler to ALL links:

    <a href="WHATEVERLINK.php" onclick="location=this.href+'?time='+timeToAttach; return false;">a</a>

    You do not need to alter your href links.

    An alternate way would be pass the time difference.

    userTimeZoneOffset = now.getTimezoneOffset();

    You would then alter the onclick handler to pass userTimeZoneOffset in place of timeToAttach.

    Yet another method would be to set a cookie using javascript, then read the cookie on the server with PHP.
    <script>
    now = new Date();
    cookievar = now.getTime();
    //or
    //cookievar = now.getTimezoneOffset();
    myCookie = "thevar="+cookievar; //read on server using cookie name "thevar"
    document.cookie = myCookie + ';';
    </script>

    Jon

  3. #3
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    great thanks for the info jon

    the time that this script gets - is it completely dependent on what the user has set their time to?

    i'm on a mac and in the top menu bar there's a clock. if i was to set it to something different, is that the time that this script would get?

  4. #4
    SitePoint Zealot jonsteele's Avatar
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think so. I know on a win platform it takes the system time from the bottom, right corner, so I guess it is the same in Mac's.

  5. #5
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    yeah it must be. it must be the time that the system time is set to.
    where else could it get the time from?
    i was just trying to work out how reliable it is.
    pretty reliable i'd say as most people have close-enough the right time on their computer
    very few people would have their time set completely wrong i'd guess.

    i haven't got round to trying this out but will v.soon thanks

  6. #6
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    that's great - worked a treat
    haven't done the getting it from within php yet but i can see it's working from the address bar
    went for the offset version (in the url, not cookie) - seemed the simplest/shortest

    is there a way to add the info to the end of each url on the page, from within that piece of code in the <head> rather than attaching individually to each url?

    because i'm only going to do this time test on the first page that the user arrives at on the site (and obviously they could arrive at any page what-so-ever).

    so is there any way to say from within the javascript in the head :
    "add
    'onclick="location=this.href+'?time='+timeToAttach; return false;">'
    to all the same-site urls on this page" ?

  7. #7
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's what I came up with (I started with some code from a post by Beetle in another thread):
    Code:
    <html>
    <head>
    <script language="javascript" type="text/javascript">
    function checkObj(obj)
    {
    	var d, toAppend;
    
    	if (obj.tagName == 'A')
    	{
    		d = new Date();
    		toAppend = 'time=' + d.getTime();
    		
    		//is there already a querystring?
    		if (obj.search.indexOf('?')==-1)
    		{
    			//create a new one from scratch
    			obj.search = '?' + toAppend;
    		}
    		else
    		{
    			//append to existing querystring
    			obj.search += '&' + toAppend;
    		}
    	}
    
    	return true;
    }
    </script>
    </head>
    <body onClick="return checkObj(event.srcElement)">
    <a href="this.php">test</a>
    </body>
    </html>
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  8. #8
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    BTW, I return "true" because so that the browser follows the (now modified) link after processing the onClick(). You don't need to modify a single <A HREF.... tag!
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  9. #9
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    great thanks marco
    haven't used it yet - will give it a go now - thanks

  10. #10
    SitePoint Zealot jonsteele's Avatar
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice! well done M@rco

    But no Netscape support...not even NS6.1...not sure about moz.
    Last edited by jonsteele; Aug 5, 2002 at 09:52.

  11. #11
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lack of NS support doesn't surprise me (sorry), but perhaps it could be tweaked to work...! I'll leave that to you!

    NS6 is Mozilla (NS just takes the source, adds their own crap to it, and recompiles) so Moz is out too.
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  12. #12
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey all. You may want to read up on how Moz handles the EVENT object, because that is the likely culprit of the incompatibility.

    I don't have the time at the moment but here is an article to get you started.
    Last edited by beetle; Aug 5, 2002 at 10:45.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  13. #13
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    hiyall

    i've given that code a go marco and it does have a slight prob :/ it doesn't work for img links - fine for text though

    BUT since, i've found this with this javascript -
    Code:
    <script type="text/javascript">
    window.onload = setLinks
    function setLinks() {
    now = new Date()
    offset = now.getTimezoneOffset();
    for ( i = 0; i < document.links.length; i++ ) {
    with ( document.links[i] ) {
    if ( href.indexOf('http://www.whatever.com/') == 0 ) {
    if ( href.indexOf('?offset') == -1 ) {
    href += '?offset=' + offset;
    } else {
    href += ';offset=' + offset;
    }
    }
    }
    }
    }
    </script>
    works with both text and img links - haven't got round to the php bit yet but this javascript certainly seems to work on my setup (mac ie5.15)

    as i mentioned i'm pretty clueless with javascript.
    is the above script - some of this part.. :
    if ( href.indexOf('http://www.whatever.com/') == 0 ) {
    if ( href.indexOf('?') == -1 ) {
    href += '?offset=' + offset;
    } else {
    href += ';offset=' + offset;
    ..trying to check to see if the offset has already been set by a previsous page? or am i misreading that? (i'm probably misreading it)
    i don't actually want to do that - check to see if the offset has been set by a previous page as it'd be better to do that server side - as that's what is going to be sending the javascript in the first place
    Last edited by johnyboy; Aug 5, 2002 at 12:30.

  14. #14
    SitePoint Zealot jonsteele's Avatar
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just be sure that even if the script does not append the time to the links in Netscape 4 or 6, the links at least DO work regularly (without the time being passed).

    This method's getting a little messy. In my opinion, the best method would be to set a cookie with the user's time offset. You'd only have to set the cookie once. Then, on the server, you can read the offset easily and using GMT get the client time.

  15. #15
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, I concur with the cookie plan - setting the time for all the links at the time the page is loaded is a bit useless, since they could take 1 minute or 1 hour to read the page, but the time reported would be the same, so that doesn't tell you a lot!

    That's why setting the time at the instant of link clicking was better, but catching the timeZoneOffset just the once and using that is by far the best.
    Last edited by M@rco; Aug 5, 2002 at 15:40.
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  16. #16
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    yup agreed - cookies

    thought of another reason to go with cookies -

    the user time is needed for one particular page on my site - if that page is the first one they go to i'm going to try and get the page to do a reload in order to get their time - and a reload wouldn't help if i was using the url method

    thanks all

  17. #17
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    what is it with netscape?

    (this is on mac)

    cookie result from ie5.15: -60
    great - i live in the uk and we're (apparently) in british summer time - that's one hour ahead of gmt so that makes complete sense

    cookie result from netscape 4.7: 1380

    what the hell?!

  18. #18
    SitePoint Zealot jonsteele's Avatar
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It says here that Netscape on Mac has a problem with this.

    If the getTimezoneOffset method is acting up, you could just subtract the local hour from GMT.

    now = new Date();
    offset = parseInt((<? echo $GMTtimeFromPHP ?> - parseInt(now.getTime()/1000))/3600);

    Not too sure though. Might get a little messy...

  19. #19
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    yeah, 1380 does mean something (i thought it was gibberish)-
    1380 divided by 60 equals 23
    with IE i got -60 which is right
    23 is a day minus an hour, or minus 60

    so all i have to do is treat different browser's respnses differently serverside - which i don't think'll be a major prob. what i need to know is how different broser/platfoms cope with the javascript now.getTimezoneOffset()

    FAVOUR REQUEST :

    could anyone let me know how the cookie offset time compares with gmt time? please?

    go here
    then reload the page so you'll see an offset number
    and let me know what the offset is and how it compares with the gmt time which is also on that page
    also if you could let me know what platform/browser/browser version you're using.

    if anyone can do this it'd be great. cheers.

  20. #20
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    gmt now: 15:56
    offset: 300

    Windows XP. IE 6. Central Time Zone
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  21. #21
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    excellent - thanks beetle just what i needed to know

    anyone else? -- more the better

  22. #22
    SitePoint Zealot jonsteele's Avatar
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    gmt now: 18:28
    offset: 240
    Eastern Time... working beautifully .

    I don't know what you did but be careful....make sure you are treating NS4 for mac and PC differently. Because on my platform, IE, NS4, NS6, and Moz all give the same offset. The difference is between mac's and pc's.

  23. #23
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    gmt now: 19:38
    offset:
    Correct (I'm in the UK), but you might want to make sure a zero is displayed instead of a blank!

    Edit:


    NOT correct.... having done my own test, have realised that -60 should be returned because of BST. Came back here and noticed that johnyboy said this earlier. Perhaps my cookie was not sent (I'm running IE6)...


    [edit take="2"]
    I'm such a dumbass... I hadn't reloaded! DOH!
    [/edit]
    Last edited by M@rco; Aug 6, 2002 at 12:41.
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  24. #24
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    but you still didn't say what the offset was marco! i guess it was -60 right? that's quite funny

    Originally posted by jonsteele
    I don't know what you did ...

    ...The difference is between mac's and pc's.
    that page i linked to doesn't process or handle it at all - it just simply repeats the number that the cookie holds - the result from your javascript jon. that's all i'm doing there.

    but the bit about the difference is between mac and pc - it's not that. i got 2 different results from netscape and explorer both on a mac.

    but i am seeing a way to normalise the results - i think

    all the results so far, have been how many minutes the user is away from gmt.

    my 2 seemingly different results are sort of the same. -60 and 1380. that's -1hour and +23hours. both those amount to the same time of day (just different days! )

    so yeah so i think i've got it now. thanks v.much for your input all

  25. #25
    SitePoint Zealot jonsteele's Avatar
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No I understand that you are getting different results in IE and NS.

    What I meant was this:

    Do not just use something like

    if($browserIsNN4){
    //blah blah
    }

    since NN4 on PC does not have a problem, and gives the same result as IE. Use:

    if($browserIsNN4 && $isMac){
    $irregularOffset = true
    }else if($browserIsNN4 && $isPC){
    $irregularOffset = false

    just a caution, since I know if you are testing on Mac it may be difficult to catch these differences.

    This link I gave eariler (http://www.xs4all.nl/~ppk/js/introdate.html#offset) says the following have a problem (or, since we just found out, give a different result):

    Netscape 2, Netscape 3 and 4 on Mac, Explorer 3 on Windows and Konqueror.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •