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.


In reply to Re: Display clock by chanio
in thread Display clock by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.