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

k, I need someone to give me an example of how to use date with cgi.

Can you give me an example that prints JUST the current date (Example, if the script were called today, it would print out "19" (Because it is April 19))?

Thanks a lot

2001-03-09: Edit by Corion: Changed title

  • Comment on Getting the date of today as a number (was: Date question)

Replies are listed 'Best First'.
Re: Getting the date of today as a number (was: Date question)
by chromatic (Archbishop) on Apr 20, 2000 at 04:11 UTC
RE: Getting the date of today as a number (was: Date question)
by Simplicus (Monk) on Apr 20, 2000 at 04:24 UTC
    The localtime(time) function returns a nine element array, the values of the array are:
       index        value
    =====================
          [0]      second
          [1]      minute
          [2]        hour
          [3]   month day 
          [4]       month
          [5]        year
          [6] day of week
          [7] day of year
          [8]       isdst : not sure what this refers to . . .
    

    There are a couple of things to watch out for in this array . . .the elements month and day are zero-based (because you'll probably use them as indexes into arrays containing month and day names . . .) and the year element has had 1900 subtracted from it, so this code:

    @timearray = localtime(time); print "$timearray[5]\n"
    will print 100, not 00 or 2000.
    Good question!
    Simplicus
      Sorry, but the above writeup has a couple flaws, and thanks to chromatic for setting me straight.
      First, the $isdst scalar is a flag for daylight savings time, 1 for yes, 0 for no, and negative is "unknown."
      Second, the year hasn't really had 1900 subtracted from it, it's really just the number of years since 1900.
      Thanks again to my Dodecatonic friend, chromatic. (I was a music composition major, once upon a time.)
Re: Getting the date of today as a number (was: Date question)
by dsdisc (Initiate) on Apr 20, 2000 at 04:01 UTC
    The simplest way would be to print:
    print scalar(localtime(time));

    Although you could format it by getting all the elements individually:
    @elements = localtime(time);

    You can easily find a reference that will tell you what each element is, just remember that months, days of year, other data is 0 based - meaning January is 0, Feb is 1, .. Dec is 11.
Re: Getting the date of today as a number (was: Date question)
by btrott (Parson) on Apr 20, 2000 at 05:31 UTC
    Or one other way, just for fun, and because I like the fact that it's way easy to remember:
    use Time::localtime; my $lt = localtime; print "Today's day of the month is ", $lt->mday;
    perldoc Time::localtime for more details.
RE: Getting the date of today as a number (was: Date question)
by Anonymous Monk on Apr 20, 2000 at 21:47 UTC
    print (localtime())[4];
      i tried yours, not working..

      then i modified yours so it looks like

      perl -e 'print ((localtime)[5])'
      and its work.

      zak