Gossamer Forum
Home : Products : DBMan : Customization :

Newbie needs help with mm/dd/yy format

Quote Reply
Newbie needs help with mm/dd/yy format
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)

Quote Reply
Re: Newbie needs help with mm/dd/yy format In reply to
You also have to edit the following codes:

Code:
$time = timelocal(0,0,0,$day, $months{$mon}, $year);

to read like the following:

Code:
$time = timelocal(0,0,0,$months{$mon}\/$day\/$year);

Hope this helps.

Regards,

------------------
Eliot Lee
Anthro TECH,L.L.C
www.anthrotech.com
----------------------


Quote Reply
Re: Newbie needs help with mm/dd/yy format In reply to
Thanks, got the field names in correct order now. But get "server error" when I try to put in the escapes as above. Tried:

$time = timelocal(0,0,0,$mon\/$day\/$year);

also tried:

$time = timelocal(0,0,0,$mon/\//$day/\//$year);

Seems like my server does not like the / as a field separator. Think this line is my only problem. Any ideas??

PS: Also tried different sub get_date posted in this forum by Iancewilson on 09/10/99, he had simular problem but wanted to use a . as a separator. Duplicated his code except for above line where I used the / as a field seperator. = same server error. Do you think I must give up on / seperator????
Quote Reply
Re: Newbie needs help with mm/dd/yy format In reply to
Sorry, above too confusing. Below is the complete code giving me the server error:

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,$months{$mon}\/ $day\/ $year);
};
if ($@) { return undef; } # Could return 0 if you want.
return ($time);
}
Quote Reply
Re: Newbie needs help with mm/dd/yy format In reply to
Also changed from above:

my ($day, $mon, $year) = split(/\//, $_[0]);
to:
my ($mon, $day, $year) = split(/\//, $_[0]);
Quote Reply
Re: Newbie needs help with mm/dd/yy format In reply to
You also have to edit the following codes:

Code:
unless ($day and $mon and $year) { return undef; }

to the following:

Code:
unless ($mon and $day and $year) { return undef; }

So, the complete date_to_unix sub-routine should look like the following:

Code:
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 ($mon, $day, $year) = split(/\//, $_[0]);
unless ($mon and $day 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,$months{$mon}\/$day\/$year);
};
if ($@) { return undef; } # Could return 0 if you want.
return ($time);
}

I REALLY hope this works.

Regards,

------------------
Eliot Lee
Anthro TECH,L.L.C
www.anthrotech.com
----------------------




[This message has been edited by Eliot (edited December 20, 1999).]
Quote Reply
Re: Newbie needs help with mm/dd/yy format In reply to
Ugggg. Pasted code, still get server error 500.
Interesting though, if I change the line:

$time = timelocal(0,0,0,$months{$mon}\/$day\/$year);

to:

$time = timelocal(0,0,0,$months{$mon},$day,$year);

It loads OK, but now the month is not displayed on the add form.
For example: 12/20/99 is displayed on add form as /20/99

Like its not recognizing the month? This may b a clue, but not for a clueless person like me.

Much Mahalo's for looking at this so much.
Quote Reply
Re: Newbie needs help with mm/dd/yy format In reply to
Copy and paste this into your script.
Code:
sub get_date {
# --------------------------------------------------------
# Returns the date in the format "dd-mmm-yy".
# Warning: If you change the default format, you must also modify the &date_to_unix
# subroutine below which converts your date format into a unix time in seconds for sorting
# purposes.
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime(time());
($day < 10) and ($day = "0$day");
$mon += 1;
($mon < 10) and ($mon = "0$mon");

$year += 1900;
return "$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) = ("Jan" => 0, "Feb" => 1, "Mar" => 2, "Apr" => 3, "May" => 4, "Jun" => 5,
"Jul" => 6, "Aug" => 7, "Sep" => 8, "Oct" => 9, "Nov" => 10,"Dec" => 11);
my ($time);
my ($mon, $day, $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) - 1900;
$time = timelocal(0,0,0,$day, $months{$mon}, $year);
};
if ($@) { return undef; } # Could return 0 if you want.
return ($time);
}
There are only two places you need to alter the code. In get_date change the way you return it and in date_to_unix change the way you split it.



------------------
WFMY-TV Webmaster

[This message has been edited by Chris071371 (edited December 22, 1999).]
Quote Reply
Re: Newbie needs help with mm/dd/yy format In reply to
Are you sure that you copy and pasted the whole routines into db.cgi? Have you altered the code anywhere else? It would help if you would post db.cgi and html.pl as a text document and give us the url. This would enable us to help you faster.

------------------
WFMY-TV Webmaster
Quote Reply
Re: Newbie needs help with mm/dd/yy format In reply to
Thank you so much for all you advice. I've made the changes, but still have the same problem.

Been trying to uderstand why, found that the add_record funtion calls the validate_record funtion which states:

push (@input_err, "$col (Invalid date format)") unless &date_to_unix($in{$col});

Guess this is why I keep getting the above message when trying to add records.

Though about changing above to check for &get_date but see that so many funtions sort on &date_to-unix so not sure what damage I'll do if I allow &get_date records to pass into the database. (seems like a lot)

Is there a way I can allow users to enter and display dates in &get_date format but acually store only &date_to_unix dates in the database?

Sorry if this is a stupid question, I'm new to cgi and DBMAN and am trying to learn as fast as I can.

Mahalo



Quote Reply
Re: Newbie needs help with mm/dd/yy format In reply to
Thanks for all you guys help.
Posted text files at:
http://aboutwaikiki.com/text/

This db.cgi has above pasted exactly. Loads OK but can't add records due to "invalid format" message from the validate_record funtion in html.pl
Quote Reply
Re: Newbie needs help with mm/dd/yy format In reply to
Well first of all, I forgot to increment the month. Try this, this should work perfectly.

Code:
sub get_date {# --------------------------------------------------------
# Returns the date in the format "dd-mmm-yy".
# Warning: If you change the default format, you must also modify the &date_to_unix
# subroutine below which converts your date format into a unix time in seconds for sorting
# purposes.
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime(time());
($day < 10) and ($day = "0$day");
$mon += 1;
($mon < 10) and ($mon = "0$mon");
$year += 1900;
return "$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 ($time);
my ($mon, $day, $year) = split(/\//, $_[0]);
unless ($day and $mon and $year) { return undef; }

use Time::Local;
eval {
$day = int($day); $year = int($year) - 1900; $mon = int($mon) - 1;
$time = timelocal(0,0,0,$day, $mon, $year);
};

if ($@) { return undef; } # Could return 0 if you want.
return ($time);
}




------------------
WFMY-TV Webmaster
Quote Reply
Re: Newbie needs help with mm/dd/yy format In reply to
Sorry, but made the change above, still same problem. When I try to add record, I get "invalid date format" error, but date is displayed correctly.

If you want to see the prob, its at:
http://aboutwaikiki.com/rmstask/db.cgi
and log in as admin/admin, then try to add a record to see error. This is a test DB for me to learn on so no danger.

made changes and posted db.txt at:
http://aboutwaikiki.com/text

Hope you don't mind my persistance on this date thing, but I really need to understand how to work with dates in DBMAN, once I get this, I'll try not to bother you again for a while.

Mahalo
Quote Reply
Re: Newbie needs help with mm/dd/yy format In reply to
You did not cut and paste what I posted above. Take out the line:

Code:
unless (defined($months{$mon})) { return undef; }

Take that line out of date_to_unix and it will work.

------------------
WFMY-TV Webmaster
Quote Reply
Re: Newbie needs help with mm/dd/yy format In reply to
YES!!!!
Success, THANK YOU so much.
You guys are the greatest!