+ Reply to Thread
Results 1 to 8 of 8

Thread: Date and Time Script

  1. #1
    secretply is offline x10Hosting Member secretply is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    50

    Date and Time Script

    I need some help with adding Date and Time to automatically update itself on the webpage based on the viewer's computer date and time. I have an idea of something like this, (left-aligned) Current Date: <Date>, (right-aligned) Current Time: <Time>, where <Date> is the date code to be updated automatically and where <Time> is the time code to be updated automatically. Any help I receive will be greatly appreciated.

  2. #2
    Scoochi2's Avatar
    Scoochi2 is offline x10 Sophmore Scoochi2 is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Southport!
    Posts
    185

    Re: Date and Time Script

    What language are you using. Furthermore, do you want <DATE> and <TIME> to be something held separately or just information for the viewer?

    If it's just information to the viewer, you can use JavaScript to get and show their time.
    HTML Code:
    <script type="text/javascript">
    <!--
    var currentTime = new Date();
    
    var hours = currentTime.getHours();
    var minutes = currentTime.getMinutes();
    var seconds = currentTime.getSeconds();
    if (hours < 10)
    hours = "0" + hours;  // JS will only show the bare minimum, but we want it to look proper. ie, no times of 1:2
    if (minutes < 10)
    minutes = "0" + minutes;
    
    var year = currentTime.getFullYear();
    var month = currentTime.getMonth() + 1; // consider month as an array starting at 0 and ending at 11
    var day = currentTime.getDate();
    if (month < 10)
    month = "0" + month;
    if (day < 10)
    day = "0" + day;
    
    document.write("TIME: " + hours + ":" + minutes + ":<sub>" + seconds +"</sub>") ;
    document.write("<br>DATE: " + day + "/" + month + "/" + year) ;
    
    
    //-->
    </script>
    The above script will output something like:
    TIME: 21:37:15
    DATE: 25/04/2009

    Just add your own styling elements and output method to get them how you want.


    If you don't want to use JS, you could use server side code to show the time.
    For example, in PHP you could find out the user's local timezone and show the server time, plus or minus whatever to get the correct time for the viewer. (for example: if your server is GMT+0200 and the viewer is GMT+0800, then show the server time + 6 hours.
    Last edited by Scoochi2; 04-25-2009 at 03:44 PM.
    If anyone can see it, my post was meant for anyone who reads it. Don't take it personally or think I'm being condescending... :nuts:

  3. #3
    secretply is offline x10Hosting Member secretply is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    50

    Re: Date and Time Script

    I have re-formatted everything to my benefit and it works perfectly. The problem is that the time doesn't automatically update itself on the webpage, only when the webpage is refreshed/reload. This might also happen to the date as well. Think you can help me for hopefully the last time right now? Thanks for all the hard work.
    Last edited by secretply; 04-25-2009 at 04:30 PM.

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Date and Time Script

    Quote Originally Posted by secretply View Post
    The problem is that the time doesn't automatically update itself on the webpage, only when the webpage is refreshed/reload.
    Use setInterval to repeatedly run a function.

    document.write is horribly outdated and won't let you update elements. Put each field in its own element (probably a <span>). Use document.getElementById to get a reference to each element in the script. Assign to element.firstChild.nodeValue to change the text (element.firstChild will be a text node, and a text node's nodeValue holds its text).

    Personally, I'm not a fan of clocks on webpages. They just clutter up the interface. If I want to check the time, my computer already displays that information.

  5. #5
    hezuo's Avatar
    hezuo is offline x10 Sophmore hezuo is an unknown quantity at this point
    Join Date
    Dec 2007
    Location
    Huascar, Santa Anita
    Posts
    174

    Re: Date and Time Script

    thanks! I was also looking for adding time into my website.

  6. #6
    quantum1's Avatar
    quantum1 is offline x10Hosting Member quantum1 is an unknown quantity at this point
    Join Date
    Sep 2008
    Location
    near Nashville, TN
    Posts
    68

    Re: Date and Time Script

    I messed around with PHP date stuff and made this little utility of dubious usefulness:

    http://quantum1.x10hosting.com/index.php?do=dates
    Two rules of development:
    1) Computers work for people; People do not work for computers
    2) Maintainability is all that matters.

  7. #7
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: Date and Time Script

    Looks good! There's this little extra output though at the beginning:

    
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  8. #8
    quantum1's Avatar
    quantum1 is offline x10Hosting Member quantum1 is an unknown quantity at this point
    Join Date
    Sep 2008
    Location
    near Nashville, TN
    Posts
    68

    Re: Date and Time Script

    Thanks for catching that. I will check out my php and post the source so you and others can comment on it. I am always learning from others. This forum is very useful for learning new ideas and techniques. :drool:
    Two rules of development:
    1) Computers work for people; People do not work for computers
    2) Maintainability is all that matters.

+ Reply to Thread

Similar Threads

  1. Multiple date & time support
    By phpasks in forum Programming Help
    Replies: 19
    Last Post: 11-28-2008, 10:55 AM
  2. Joomla! 1.5.8 came out! Time to update!
    By Noodles8 in forum Off Topic
    Replies: 4
    Last Post: 11-13-2008, 05:14 PM
  3. A script to ping servers from a DB
    By -fedexer- in forum Programming Help
    Replies: 18
    Last Post: 04-05-2008, 11:38 AM
  4. date and time
    By cuteboytm in forum Scripts & 3rd Party Apps
    Replies: 9
    Last Post: 10-22-2007, 09:11 AM
  5. Replies: 4
    Last Post: 09-04-2005, 03:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers