Forum Moderators: open

Message Too Old, No Replies

2 bits of help for novice coder.

         

Pablo84

5:39 pm on Apr 29, 2009 (gmt 0)

10+ Year Member



I'm fairly new to coding and seem ti be winging it a bit. I would like to know how to

A) Create my own date and time without having to get one of those free ones from some site.

B) Have it so my marquee scrolling text can be downloaded as a ticker, I'm sort of aware that the marquee thing may not be the right way to go about things.

Cheers

P84

I'm absolutey struggling with css presentation and use html and some css inline!

punisa

11:19 am on Apr 30, 2009 (gmt 0)

10+ Year Member



Take it easy Pablo, starting with coding can be very frustrating and time consuming. But eventually you will learn more and more and you'll end up with a great deal of knowledge that will be very valuable in your carrer : )

Ok, enough for the welcome. Let's see your problems..

A) Don't make a habit of using free stuff and similar scripts. Why? You'll find out that it gets very problematic when you need to do some changes, even small ones.

You can create date and time using php, such as:


// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A');

Google "php date" and use php.net page for a complete reference on date, time etc..

B) If you actually want a clock that goes "tick-tack" on seconds, then you'll have to use javascript for that, I'll give you the code I use:

1. Put this part where you want the clock to be:


<span id="clock">&nbsp;</span>

2. Put this at the bottom of your code (under HTML,BODY and everything):

<script type="text/javascript">
window.onload = function(){
updateClock();
setInterval('updateClock()', 1000 );
}
function updateClock ( )
{
var currentTime = new Date ( );
var currentHours = currentTime.getHours ( );
var currentMinutes = currentTime.getMinutes ( );
var currentSeconds = currentTime.getSeconds ( );
// Pad the minutes and seconds with leading zeros, if required
currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
// Choose either "AM" or "PM" as appropriate
var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";
// Convert the hours component to 12-hour format if needed
currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;
// Convert an hours component of "0" to "12"
currentHours = ( currentHours == 0 ) ? 12 : currentHours;
// Compose the string for display
var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
// Update the time display
document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}
</script>

3. You can style the way your clock looks by using css styling (Don't use inline!), set it like this in your head part, just under your title and other meta tags:


<style type="text/css">
<!--
#clock {
font-family: Verdana, Geneva, sans-serif;
font-size: 12px;
font-weight: bold;
color: #069;
}
-->
</style>

Try it out and feel free to ask about any of the steps if you need more information.
Good luck and welcome : )

Pablo84

2:04 pm on Apr 30, 2009 (gmt 0)

10+ Year Member



I delved into css last night and well...I'm sold. I did in 20 lines what took me 150 lines in html. I can see the benefit of having the presentation form somewhere else, especially if you have a ton of content.

I decided against the scrolling text thing and am looking at rss feeds instead.

As for the clock I will put the code in and try to work out what each part does.
Thanks for welcome and reply, i've been a lurker on here for way too long.

Yours very grateful
P84

Pablo84

2:14 pm on Apr 30, 2009 (gmt 0)

10+ Year Member



Just put the clock in

PERFECT!

Thanks Punisa A*

punisa

2:28 pm on Apr 30, 2009 (gmt 0)

10+ Year Member



Glad it helped :)

g1smd

10:20 pm on Apr 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



To de-clutter your HTML page even more, you can move the main chunks of both CSS and JS into their own respective external files, with a single line of code in the HTML file to call each of them in.

brodyh

1:10 am on May 11, 2009 (gmt 0)

10+ Year Member



Just curious, why would you put those snippets underneath the <body> and <html> tags? Whenever I have page-specific JavaScript (usually to call a function in an external file) I just put it right before the </body> tag.. so that it's not in any of my <div> elements but still in the <body>.

I can kind of understand why you'd want it out of the <body>, but why also <html>? Any insight into this would be great.