SitePoint Sponsor |
|
User Tag List
Results 1 to 4 of 4
-
Jan 7, 2005, 23:51 #1
- Join Date
- Jan 2004
- Location
- Seattle
- Posts
- 4,328
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Using JavaScript to display date and time
I posted this on the PHP forum and was advised to seek a JavaScript solution.
* * * * * * * * * *
If you look in the top right corner of my page at http://www.politix.us/world/eur/afg/index.php you'll see a PHP function that displays the date and time. This is the code behind it:
PHP Code:<div id="pagedate"> (<em><?php echo date("l"); ?></em>) <?php echo date("F d, Y"); ?> <span id="pagetime"><?php echo date("h:i:s A"); ?></span></div>
1. How can I make it dynamic, so that the minutes and seconds change even if a page isn't refreshed?
2. How can I make it display the VIEWER'S local time? It shows midnight when it's 9 p.m. my time, or something like that.
Also, would displaying seconds eat up a lot of bandwidth? If so, I might limit it to minutes.
Thanks.
-
Jan 8, 2005, 02:02 #2
- Join Date
- Dec 2002
- Location
- Alabama, USA
- Posts
- 2,560
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi geosite, this is a quick example but should give you some ideas:
Code:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html> <head> <script type='text/javascript'> window.onload = function() { if (document.getElementById) { rtClock(); } } var dayOfWeek = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'); function rtClock() { var m = 'AM', now = new Date(); setTimeout('rtClock()', 1000); var ele = document.getElementById('clock'); var hours = now.getHours(); if (hours == 0) hours = 12; else if (hours == 12) m = 'PM'; else if (hours > 12) { hours -= 12; m = 'PM'; } ele.innerHTML = dayOfWeek[now.getDay()] + ', ' + hours + ':' + now.getMinutes() + ':' + now.getSeconds() + ' ' + m; } </script> </head> <body> <div id='clock'></div> </body> </html>
Cross-Browser.com, Home of the X Library
-
Jan 8, 2005, 02:02 #3
- Join Date
- Oct 2004
- Location
- New Jersey
- Posts
- 235
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You can also look for something in here...
http://www.hotscripts.com/JavaScript...ime/index.html
-
Jan 8, 2005, 03:04 #4
- Join Date
- Jan 2004
- Location
- Seattle
- Posts
- 4,328
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks for the tips. I may use PHP to display the date, since it's so much simpler, then use JavaScript to make a dynamic time display.
Bookmarks