Gossamer Forum
Home : Products : DBMan : Customization :

Need help with 'sub date_to_unix' conversion

Quote Reply
Need help with 'sub date_to_unix' conversion
The original DBMan script has the date as 01-May-2000. I want the date to be 05/01/2000. I have coded the 'sub get_date' to give me the 05/01/2000. But when I go to add a record, the validate routine can't convert the date back to unix time and gives me back an error on the date field. Here is my 'sub date_to_unix:

Code:
sub date_to_unix {
#------------------------------------
my ($date) = $_[0];
my ($time);
my ($mon, $day, $year) = split(/\//, $_[0]);
unless ($day and $mon and $year) { return undef; }
use Time::Local;
eval {
$mon = int($mon) - 1; $day = int($day); $year = int($year) - 1900;
$time = timelocal(0,0,0,$day, $mon, $year);
};
if ($@) { return undef; } # Could return 0 if you want.
return ($time);
}

As you can see, I have changed the
my ($day, $mon, $year) = split(/-/, $_[0]);
to
my ($mon, $day, $year) = split(/\//, $_[0]);

and changed the $mon in eval to
$mon = int($mon) - 1;

Any suggestions?

AJ
Quote Reply
Re: Need help with 'sub date_to_unix' conversion In reply to
Your code works fine when I run it through Perl on my system.

To do a little debugging, just add, someplace where you will see it (on sub html_home or something)

Code:
$date = &date_to_unix("05/01/2000");
print "--$date--";

The dashes are there so you can see where it would be in case nothing comes back.

If something does come back, there's another reason that you're getting an invalid date.


------------------
JPD






Quote Reply
Re: Need help with 'sub date_to_unix' conversion In reply to
JPDeni,

Thanks for testing it. I'll try your suggestion and dig deeper!

AJ
Quote Reply
Re: Need help with 'sub date_to_unix' conversion In reply to
You're right. It does work. For some reason, I left the:

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

code in the sub and when I took it out it added a record. Thanks.

AJ