Web Analytics Made Easy -
StatCounter $env{'date_gmt'}; - CodingForum

Announcement

Collapse
No announcement yet.

$env{'date_gmt'};

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • $env{'date_gmt'};

    I have been using $date=$ENV{'DATE_GMT'} in several Perl programs that are called as Server Side Includes with no problem. I am now trying to use it in Perl program that creates the entire html text and it refuses to work. EG, in the test program below I get the result date=none (so $ENV{'DATE_GMT'} must be empty). Yet this code is edited from a counter I wrote that is on the same server, that happily collects and logs the date for me. The only difference is that the counter is pulled via SSI to a .shtml page, whereas this code is stand alone (I.E. in the form http://www.site.com/cgi-bin/test.cgi). What am I doing wrong ?

    #!/usr/bin/perl

    $gmtdate = $ENV{'DATE_GMT'};

    # check ENVs are not empty
    if ($gmtdate eq "") {$gmtdate = "none"};

    print "Content-type: text/html\n\n";
    print "date=$gmtdate";

  • #2
    This is the method that I use to get the time and date:

    Code:
    ($second, $minute, $hour, $day, $month, $year, $weekday, $dayofyear, $isdst) = localtime(time);
    $dayofyear++;
    if ($day < 10) { $day = 0 . $day; }
    if ($month < 10) { $month = 0 . $month; } $month++;
    $hour   = &hours($hour);
    $minute = &minutes($minute);
    $year     += 1900;
    $site_time = "$hour:$minute (GMT)";
    $site_date = "$year/$month/$day";
    
    sub minutes {
    	$minute = shift;
    
    	if ($minute < "10") {
    	 $minute = 0 . $minute;
    	 return $minute;
    	} else {
    	 return $minute;
    	}
    }
    
    sub hours {
    	$hour = shift;
    
    	if ($hour > 15) {
    	 $hour += 8;
    	 if ($hour > 24) {
    	  $hour -= 24;
    	 }
    	return $hour;
            } else {
    	 $hour += 8;
    	return $hour;
    	}
    }
    Edit: This converts the server time (USA) to GMT time (because I am from the UK )
    There is probably a much better way, but this allows me to use seperate values in strings (such as $minute, $hour etc).

    -Stu
    if ($ENV{'QUERY_STRING'} eq "Afrow UK") {
    print "$ENV{'QUERY_STRING'} rocks!";
    } else {
    print qq~$ENV{'QUERY_STRING'} sucks :)~;
    }

    Comment


    • #3
      Afrow UK has the right idea for extracting the date and time values. Perl, unlink PHP, does not allow conveniant date formattting.

      if you just want date you could do something like:

      ($day, $month, $year) = localtime[3..5];

      and then process them the same way as Afrow did.

      the printf function offers some useful formatting.

      YYYY MM DD format:
      ($day, $month, $year) = localtime[3..5];
      printf("%04d %02d %02d\n", $year+1900, $month+1, $day);
      "There is more than one way to do it."

      Comment


      • #4
        Thanks guys, I have done as you suggested but used gmtime(time) instead. I don't know why I did not think of that, my mind was so set on fixing the env problem that it did not cross my mind to try another method. Must remember my mantras, five times before bed "there's more than one way to do it".

        Comment


        • #5
          Originally posted by Paul-Ecchi
          Thanks guys, I have done as you suggested but used gmtime(time) instead. I don't know why I did not think of that, my mind was so set on fixing the env problem that it did not cross my mind to try another method. Must remember my mantras, five times before bed "there's more than one way to do it".
          W00T!
          Never knew about gmttime(time) (is it gmttime or gmtime?)

          -Stu
          if ($ENV{'QUERY_STRING'} eq "Afrow UK") {
          print "$ENV{'QUERY_STRING'} rocks!";
          } else {
          print qq~$ENV{'QUERY_STRING'} sucks :)~;
          }

          Comment


          • #6
            Its gmtime(time) and works exactly the same as localtime(time) except that the results are in G.M.T (note that this is not always the same as localtime(time) in places on the meridian because G.M.T. does not change for summer)

            Comment

            Working...
            X