Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Display clock

by chanio (Priest)
on Sep 14, 2005 at 19:00 UTC ( #491973=note: print w/replies, xml ) Need Help??

in reply to Display clock

Simply, provide the server's starting date in the javascript (see the example)! You need to write it with the expected format. Then, every page would start from that date, instead of from the user's one. Remember that javascript starts when the user loads the page. But, the server does all, before that.
function timeNow() { //Shows the server's time and formats it into hh:mm:ss am/pm f +ormat. now = new Date($REPLACE_WITH_THE_SERVER'S_DATE) ...
<UPDATED> You should write your code into a $scalar variable and first have your $REPLACE_WITH..._DATE loaded with your desired date (with the scalar localtime function). See the following one-liner (you can try it, first)...
perl -e'print "Wed Sep 14 23:22:59 EDT 2005\n";my @ti=split(/\s+/,scal +ar localtime);my $year=pop(@ti);push(@ti,"EDT",$year);print join(" ", +@ti),"\n"' Wed Sep 14 23:22:59 EDT 2005 Mon Sep 26 21:01:16 EDT 2005
Try it several times and you'll see that it changes to your actual date!

You could do it better, for sure! But this simply shows how you could have the same format as the one needed for the js new Date() format...

Why using such a horrible and long variable instead of writing the time? Because you are going to run the cgi script every time that a new user calls it. So you should display the actual server's time, and not the old one, Ok?

If you need more acuracy, you should use:scalar localtime(time + $elapsed_seconds), where $elapsed_seconds would have the time that elapsed from the time when the server sends the html page to the time when the user sees it :). (TODO: read about perldoc POSIX and perldoc -f localtime)

NOTICE: All these code intends to start the user's JS clock at the exact time that the user asks the page. If the clock starts at the correct time, it should update every second (depends on the js code, not on the server's code any more) to keep on being correct until the user leaves the page. What happens if the user saves a copy of the html page at his PC? Well, the clock would start again but with the old time, because the server does not display that page again for the user. But there are tricks to do it again, this way, also. Find out more about the "Web Site Story" somewhere where you could find javascript code, it is a rating technique.

Replies are listed 'Best First'.
Re^2: Display clock
by Anonymous Monk on Sep 15, 2005 at 02:17 UTC
    I followed your instruction and modified the function as follows.
    function timeNow() { //Grabs the current time and formats it into hh:mm:ss am/pm fo +rmat. // now = new Date() now = new Date('Wed Sep 14 23:22:59 EDT 2005') hours = now.getHours() minutes = now.getMinutes() seconds = now.getSeconds() timeStr = "" + ((hours > 12) ? hours - 12 : hours) timeStr += ((minutes < 10) ? ":0" : ":") + minutes timeStr += ((seconds < 10) ? ":0" : ":") + seconds timeStr += (hours >= 12) ? " PM" : " AM" return timeStr }
    This data (assume from Server) is for testing purpose. But the Clock display will not change. Clock display as 11:22:59 PM. How can I display the running clock?
    Please correct me if I am wrong.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://491973]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2017-11-21 21:36 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (312 votes). Check out past polls.