Forum Moderators: coopster & phranque

Message Too Old, No Replies

How to get the time off the server

DATE_GMT not a standard enviroment variable?

         

bill

6:01 am on Sep 18, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Let me preface this by saying I'm completely clueless. I have zero experience with this, so be gentle.

I am using a free script called AlienForm [cgi.tj] that processes form input and sends off e-mail. I've got the script working fine on a couple of different servers (Apache *nix), but can't seem to get the time off of any of them. In the script documentation they show an example using DATE_GMT [cgi.tj] to get the time off the server, but I've yet to get it to work for me. I sent an e-mail to my host asking why this didn't work, and they responded that DATE_GMT is not a standard CGI environment variable, but there should be other ways of accomplishing the same thing...

My question is, using this script, how could I get the current time off the server without access to DATE_GMT or DATE_LOCAL, neither of which seem to be available to me?

mdharrold

10:24 pm on Sep 18, 2001 (gmt 0)

10+ Year Member



"localtime ()" returns the server's time. This will probably give you what you want.

But if you want to manipulate what Perl gives you, go to About.com's Perl section [perl.about.com].

volatilegx

10:44 pm on Sep 18, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's a little formula for determining time:

#determine local time
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
$isdst="";$wday=""; # second use of vars to stop errors
if (length ($hour) eq 1) {$hour= "0".$hour;}
if (length ($min) eq 1) {$min= "0".$min;}
if (length ($sec) eq 1){$sec= "0".$sec;}
if (length ($mon) eq 1) {$mon= "0".$mon;}
if (length ($mday) eq 1) {$mday= "0".$mday;}
$mon++;
$year =~ s/.*(..)$/$1/;
if ($year < 90) { $year = "20" . $year; }
$nowdate = "$year-$mon-$mday $hour:$min:$sec";

Brett_Tabke

11:12 pm on Sep 18, 2001 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Open up the main af script in a text editor.
Find the line

print MAIL @_;

On the next line, insert the code that volatilegx just showed you.
And add one more line:

print MAIL $nowdate;
When done, the subroutine will look like:
sub SendMail {
if ($smtp_server) { SendMailSMTP(@_) }
else {
# check that mailer exists and is executable:
unless(-X $mail_cmd) {Error('Mailer Command Error', "The mailer '$mail_cmd' does not exist, or is not executable."); return}
open(MAIL,"¦$mail_cmd $mail_flags") ¦¦ Error('Mailer Open Error',"An error occurred when trying to open the mailer ($mail_cmd): $!.");
print MAIL @_;
Here's a little formula for determining time:
#determine local time
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
$isdst="";$wday=""; # second use of vars to stop errors
if (length ($hour) eq 1) {$hour= "0".$hour;}
if (length ($min) eq 1) {$min= "0".$min;}
if (length ($sec) eq 1){$sec= "0".$sec;}
if (length ($mon) eq 1) {$mon= "0".$mon;}
if (length ($mday) eq 1) {$mday= "0".$mday;}
$mon++;
$year =~ s/.*(..)$/$1/;
if ($year < 90) { $year = "20" . $year; }
$nowdate = "$year-$mon-$mday $hour:$min:$sec";
print MAIL $nowdate;
close(MAIL) or Error('Mail Send Error',"An error occurred when sending the email: $?. Please check the email's headers.");
}
}

bill

1:11 am on Sep 20, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



volatilegx and Brett thank you very much for the assistance. I now get the local server time printed on the bottom of all my templates.

Could I ask for a little more guidance? The hosting company I'm currently using has its servers somewhere around Puerto Rico...the webs I'm taking care of are for Japan and China...would it be possible to add a few hours to this hack? For example, Japan is GMT +9, would it be possible to adjust this script to reflect Japan time?

bill

2:01 am on Sep 25, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



bump

Is there any way I can manipulate the localtime to display a certain offset of hours?

sugarkane

8:27 am on Sep 25, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yup. Replace
[perl]
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
[/perl]
with
[perl]
$offset="9";
$time=time+($offset*3600);
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($time);
[/perl]

where $offset is the number of hours + or -

bill

12:08 am on Sep 26, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



sugarkane [big]Thank you[/big] thank you thank you! That worked perfectly. :)

I hate to admit it, but I believe I was the inspiration for the 99.99% of High School Seniors Can't Read Perl... [webmasterworld.com] article.

littleman

12:15 am on Sep 26, 2001 (gmt 0)



wmw is the amateur scripter's best friend. :)