Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help adding am/pm to script please

Status
Not open for further replies.

Daffydd

Technical User
Mar 28, 2003
6
US
Hi all.
I needed a "last modified" script for my website. I found this one:


# Configuration
@months = ("January","February","March","April","May","June",
"July","August","September","October","November","December");

#################################
# Executable
$ENV{"TZ"} = "CST6CDT";
# Get the input
$link = $ENV{'DOCUMENT_URI'};
$root = $ENV{'DOCUMENT_ROOT'};

if (substr($link, 1, 1) eq '/') {
$link = substr($link, 1, length($link) - 1);
}
if (substr($root, length($root), 1) eq '/') {
$root = substr($root, 0, length($root) - 1);
}


### Calculating the $date ###

($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat("$root/$link");
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($mtime);
$time = sprintf("%02d",$min);
$year += 1900;
$date = "$months[$mon] $mday, $year at $hour:$time";

print "Content-Type: text/html\n\n$date";


My question is:
How do I add am/pm to the time display?
Thanks!
Daf
 
Code:
    if (length $min == 1) {$min= "0".$min;}
    if (length $sec == 1) {$sec= "0".$sec;  }
    if ($hour ge "12") {
       $hour-=12;
       $ampm = "PM"; 
    } else {
       $ampm = "AM";
    }
    if (length $hour == 1) {$hour= "0".$hour}
    if ($hour eq "00") { $hour="12"; }
print "$hour:$min:$sec:$ampm";
 
Hey thanks Paul!
Could I impose upon you further by asking exactly where that would need to be inserted? I'm just not very fluent in Perl...

Thanks
Daf
 
Code:
if ($hour ge "12") {
This comparison should be numeric.
Code:
if ($hour >= "12") {
Here is an alternative (slightly more compact)
Code:
$time = sprintf "%02d:%02d:%02d %s", $hour%12, $min, $sec, ('AM','PM')[$hour>12];
You can replace this line
Code:
$time = sprintf("%02d",$min);
with the above example.

jaa
 
Slight modification to make 0,12,and 24 all print as 12:
Code:
my $time = sprintf "%02d:%02d:%02d %s", $hour%12||12, $min, $sec, ('AM', 'PM')[$hour>12];
jaa
 
Thanks-
here is what I get when I replace the existing line with the modified line:


There seems to be - hours, minutes, seconds and ?
but the am/pm is working fine.
Would you please help me eliminate the ? and seconds so that I end up with something like this:

Mar. 29, 2003 at 19:07 pm

I would prefer the abreviated Mon.
Sorry to be so picky and so much trouble.
I appreciate the help!
Below is the modified script.

Thanks
Daf


# Configuration
@months = ("January","February","March","April","May","June",
"July","August","September","October","November","December");

#################################
# Executable
$ENV{"TZ"} = "CST6CDT";
# Get the input
$link = $ENV{'DOCUMENT_URI'};
$root = $ENV{'DOCUMENT_ROOT'};

if (substr($link, 1, 1) eq '/') {
$link = substr($link, 1, length($link) - 1);
}
if (substr($root, length($root), 1) eq '/') {
$root = substr($root, 0, length($root) - 1);
}


### Calculating the $date ###

($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat("$root/$link");
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($mtime);
$time = sprintf "%02d:%02d:%02d %s", $hour%12, $min, $sec, ('AM','PM')[$hour>12];
$year += 1900;
$date = "$months[$mon] $mday, $year at $hour:$time";

print "Content-Type: text/html\n\n$date";
 
Figured it out! Thanks for all the help!
Daf
 
You'll want to remove the $hour from this line:
Code:
$date = "$months[$mon] $mday, $year at $hour:$time";

because $time now contains all of hour,min,sec and am/pm.
Code:
$date = "$months[$mon] $mday, $year at $time";
Another thing that I realized was that to get 12 noon to be properly labeled as PM a slight modification is necessary to the end of the $time assignment. Change
Code:
('AM','PM')[$hour>12]
# To
('PM','AM')[$hour<12]
# Or
('AM','PM')[$hour>=12]
jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top