> ### get time
> my($sec, $min, $hour, $mday, $month, $year) = (localtime)[0..5];
> $year += 1900;
> $mday = "0" . $mday if $mday < 10;
> $month++; # perl counts from -1 on occasion
> $month = "0" . $month if $month < 10;
> ####
>
> -- later in the same file --
>
> print TOFILE "On $month/$mday/$year At $hour:$min you wrote:<br>\n\n";
>
> how do I use print to provide a leading '0' to $min, such that
> I get 5:01 and not 5:1

There were several correct responses utilizing printf. I just wanted to
mention that you have another option. You already have two lines of code
that look like this:

$month = "0" . $month if $month < 10;
$mday = "0" . $mday if $mday < 10;

adding

$min = "0" . $min if $min < 10;

will pad your minutes with a 0. I have used both methods for various
reasons. You probably only need to use one or the other. I would use the
printf unless you have some need to have the variables padded with 0's.

Herb Hall


Reply via email to