SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist N9ne's Avatar
    Join Date
    Aug 2002
    Location
    UK
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I display the date and time of the viewing user to him?

    I want to be able to display the time and date that the viewer has set on their computer, i.e. their local time, not the server's, how would I go about doing that?

  2. #2
    SitePoint Zealot evolvemedia.com's Avatar
    Join Date
    May 2003
    Location
    Orange County, California
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a javascript that will do the trick! It shows a "live" clock in newer browsers, a static clock in older browsers.

    Add the following code to your body tag.

    onLoad="goforit()" Example: <body <body bgcolor="#003366" text="#000000" onLoad="goforit()">

    Then, insert the following code where you want your date/clock to show up.

    <script>

    var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
    var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December")

    function getthedate(){
    var mydate=new Date()
    var year=mydate.getYear()
    if (year < 1000)
    year+=1900
    var day=mydate.getDay()
    var month=mydate.getMonth()
    var daym=mydate.getDate()
    if (daym<10)
    daym="0"+daym
    var hours=mydate.getHours()
    var minutes=mydate.getMinutes()
    var seconds=mydate.getSeconds()
    var dn="AM"
    if (hours>=12)
    dn="PM"
    if (hours>12){
    hours=hours-12
    }
    if (hours==0)
    hours=12
    if (minutes<=9)
    minutes="0"+minutes
    if (seconds<=9)
    seconds="0"+seconds
    //change font size here
    var cdate="<small><font color='000000' face='Arial'><b>"+dayarray[day]+", "+montharray[month]+" "+daym+", "+year+" "+hours+":"+minutes+":"+seconds+" "+dn
    +"</b></font></small>"
    if (document.all)
    document.all.clock.innerHTML=cdate
    else if (document.getElementById)
    document.getElementById("clock").innerHTML=cdate
    else
    document.write(cdate)
    }
    if (!document.all&&!document.getElementById)
    getthedate()
    function goforit(){
    if (document.all||document.getElementById)
    setInterval("getthedate()",1000)
    }

    </script>
    <span id="clock"></span>

    Enjoy!
    EvolveMedia.com - Web Branding Multimedia Programming & more.
    Multi-Whois - Checks com, net, org, us, info, biz, & uk.
    Small Links.com - Convert long links into small links for free!

  3. #3
    SitePoint Evangelist N9ne's Avatar
    Join Date
    Aug 2002
    Location
    UK
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's fantastic! Excellent, thanks very much .

  4. #4
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    one point I should make though. This clock is controled by the clock in the user computer, so that if the user computer's clock is wrong, the time on the website will be too...

    PHP clock would not update itself though since php is server side, (although that is propably doable with javascript+php (letting javascript increment the time every second)), take a look at date() function.
    - website

  5. #5
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by website
    PHP clock would not update itself though since php is server side, (although that is propably doable with javascript+php (letting javascript increment the time every second)), take a look at date() function.
    Yeah, but without some really hardcore coding, it would display the servers time and not the users

  6. #6
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DJ P@CkMaN
    Yeah, but without some really hardcore coding, it would display the servers time and not the users
    One could use geo-targetting and determine the user's time zone, then use that as an offset.
    TuitionFree a free library for the self-taught
    Anode Says... Blogging For Your Pleasure

  7. #7
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well I did say it was doable
    - website


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
  •