Aloha from Hawaii. New to cgi and dbman, pls be kind.
Trying to format date to show like this: 12/20/99
Loaded bug fix mod and search last 100 days of forum. Here is what I came up with:
sub get_date {
# Returns the date in the format "mm-dd-yy".
#######################################################################################
# #
# Warning: If you change the default format, you must also modify the &date_to_unix #
# subroutine which converts your date format into a unix time in seconds for sorting #
# purposes. #
# #
# If you have changed the date format in sub date_to_unix, be sure to make appropriate#
# changes here. The structure of the date must be identical between sub date_to_unix #
# and sub get_date. #
# #
#######################################################################################
my ($time1) = $_[0];
($time1) or ($time1 = time());
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime($time1);
my (@months) = qw!01 02 03 04 05 06 07 08 09 10 11 12!;
($day < 10) and ($day = "0$day");
$year = $year;
return "$months[$mon]\/$day\/$year";
}
sub date_to_unix {
# --------------------------------------------------------
# This routine must take your date format and return the time a la UNIX time().
# Some things to be careful about..
# int your values just in case to remove spaces, etc.
# catch the fatal error timelocal will generate if you have a bad date..
# don't forget that the month is indexed from 0!
#
my ($date) = $_[0];
my (%months) = ("01" => 0, "02" => 1, "03" => 2, "04" => 3, "05" => 4, "06" => 5,
"07" => 6, "08" => 7, "09" => 8, "10" => 9, "11" => 10,"12" => 11);
my ($time);
my ($day, $mon, $year) = split(/\//, $_[0]);
unless ($day and $mon and $year) { return undef; }
unless (defined($months{$mon})) { return undef; }
use Time::Local;
eval {
$day = int($day); $year = int($year);
$time = timelocal(0,0,0,$day, $months{$mon}, $year);
};
if ($@) { return undef; } # Could return 0 if you want.
return ($time);
}
Looks good, compiles OK finally, displays OK, but when I go to add new records with this format, it errors with "invalid date format"
What am I doing wrong??
Mahalo (Thanks)
Trying to format date to show like this: 12/20/99
Loaded bug fix mod and search last 100 days of forum. Here is what I came up with:
sub get_date {
# Returns the date in the format "mm-dd-yy".
#######################################################################################
# #
# Warning: If you change the default format, you must also modify the &date_to_unix #
# subroutine which converts your date format into a unix time in seconds for sorting #
# purposes. #
# #
# If you have changed the date format in sub date_to_unix, be sure to make appropriate#
# changes here. The structure of the date must be identical between sub date_to_unix #
# and sub get_date. #
# #
#######################################################################################
my ($time1) = $_[0];
($time1) or ($time1 = time());
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime($time1);
my (@months) = qw!01 02 03 04 05 06 07 08 09 10 11 12!;
($day < 10) and ($day = "0$day");
$year = $year;
return "$months[$mon]\/$day\/$year";
}
sub date_to_unix {
# --------------------------------------------------------
# This routine must take your date format and return the time a la UNIX time().
# Some things to be careful about..
# int your values just in case to remove spaces, etc.
# catch the fatal error timelocal will generate if you have a bad date..
# don't forget that the month is indexed from 0!
#
my ($date) = $_[0];
my (%months) = ("01" => 0, "02" => 1, "03" => 2, "04" => 3, "05" => 4, "06" => 5,
"07" => 6, "08" => 7, "09" => 8, "10" => 9, "11" => 10,"12" => 11);
my ($time);
my ($day, $mon, $year) = split(/\//, $_[0]);
unless ($day and $mon and $year) { return undef; }
unless (defined($months{$mon})) { return undef; }
use Time::Local;
eval {
$day = int($day); $year = int($year);
$time = timelocal(0,0,0,$day, $months{$mon}, $year);
};
if ($@) { return undef; } # Could return 0 if you want.
return ($time);
}
Looks good, compiles OK finally, displays OK, but when I go to add new records with this format, it errors with "invalid date format"
What am I doing wrong??
Mahalo (Thanks)