cmshowers has asked for the wisdom of the Perl Monks concerning the following question:

I use localtime() to display the time on my websites, record when posts to my message boards were made, and many more purposes probably even when I should be using gmtime. Imagine my surprise when I discovered there is no analogous function in php! This got me wondering why and I found the explanation that php runs on the server side and therefore has no knowledge of the time on the user's machine. But wait. How could Perl possible know then? I tried looking at the headers being sent over guessing something was hidden in there, but I couldn't find anything. So how does Perl get the time off the user's machine?

CS
  • Comment on how does localtime() get the time from the user's machine?

Replies are listed 'Best First'.
Re: how does localtime() get the time from the user's machine?
by friedo (Prior) on Mar 15, 2006 at 22:03 UTC
    localtime gets the time on the machine it is running on. It has nothing to do with CGI or web browsers and whatnot.
Re: how does localtime() get the time from the user's machine?
by ikegami (Patriarch) on Mar 15, 2006 at 22:21 UTC

    The immediate and relevant user of localtime is the Perl script. It's not the web server on the other side of the CGI interface. It's not the web client on the other side of the HTTP connection. etc. localtime deals with the time zone specified by the script, which defaults to the time zone of the machine on which the script runs.

    It may be possible to fetch the host's time zone using JavaScript. It's definitely possible by embedding an ActiveX object (if the web browser supports and allows it). And of course, you could ask for it (like Perl Monks does). If this info is sent to the Perl script, then it could ask localtime to work in the web browser's time zone. But that's not an automatic process.

Re: how does localtime() get the time from the user's machine?
by TedPride (Priest) on Mar 15, 2006 at 22:47 UTC
Re: how does localtime() get the time from the user's machine?
by cmshowers (Initiate) on Mar 15, 2006 at 23:43 UTC
    Okay, I just went back and looked at my time function I always use and it adjusts for the fact that my servers are always in California (Pacific time +10800 equals Eastern time). Amazing that I could've forgot I did that and it's always worked out with the servers being in the same timezone.

    I'd like to thank everyone who replied and I appreciate the JavaScript timezone discovery idea. I also agree about php's time functions being robust. I simply used
    date("M j, Y g:i A", time())
    to generate the readable time whereas my old Perl function I wrote long ago is about 20 lines long to do the same thing!

    Thanks again,
    CS
Re: how does localtime() get the time from the user's machine?
by TedPride (Priest) on Mar 16, 2006 at 20:10 UTC
    You can do the same thing in Perl in a couple lines. It's just not as pretty.