Gossamer Forum
Home : Products : DBMan : Customization :

How to change date format

Quote Reply
How to change date format
I would like to change the date format to something like this.
Wed-07-Nov-2001.
I looked at the db.pl and found the date routines, but did not know what to change or add.Blush

Quote Reply
Re: [amfreak] How to change date format In reply to
In sub get_date, just change the "return"-line:

return "$dweek-$day-$months[$mon]-$year";

Then you can get the current date in that format by calling:

my $date = &get_date;

kellner
Quote Reply
Re: [kellner] How to change date format In reply to
ok this is what I did. but it didnt work.

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());
my (@months) = qw!Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec!;
($day < 10) and ($day = "0$day");
$year = $year + 1900;

return "$dweek-$day-$months[$mon]";
}

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 ($date) = &get_date;
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 ($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) - 1900;
$time = timelocal(0,0,0,0,$dweek, $day, $months{$mon}, $year);
};
if ($@) { return undef; } # Could return 0 if you want.
return ($time);
}
Quote Reply
Re: [amfreak] How to change date format In reply to
ARGH, I had forgotten about date_to_unix.

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 ($dweek, $day, $mon, $year) = split(/-/, $date);
unless ($dweek and $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,0,$dweek, $day, $months{$mon}, $year);
};
if ($@) { return undef; } # Could return 0 if you want.
return ($time);
}[/reply]
Note the bold parts.
In date_to_unix, you shouldn't add my ($date) = &get_date; . This sub is used for validation of dates and needs to work with an input value, not with just today's date.
Perhaps you might want to run additional checks on the day of the week - similar to checks on months -; the above just checks whether it's there.
Note that this will *force* any date input to contain a weekday.
kellner
Quote Reply
Re: [kellner] How to change date format In reply to
Thanks for pusing me in the right direction kellner. Wink
Ok I got it to ALMOST work. It shows the date correctly but when I try to save it says

Date (Invalid date format)

I just need the last push in the right direction to get this done.
This is what I had to add to get it to work so far. The bold is what I added.
When this is all done some one should add this to a database somewhere. I searched all the fourms and found nothing on how to do this!

sub get_date {
# --------------------------------------------------------
# Returns the date in the format "day of the week name-dd-mmm-yy" ex-.Sat-16-Nov-2001
# 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());
my (@months) = qw!Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec!;
my (@dwk) = qw!Mon Tue Wed Thur Fri Sat Sun!;
($day < 10) and ($day = "0$day");
$year = $year + 1900;

return "$dwk[$dweek]-$day-$months[$mon]-$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 (%dwk) = ("Mon" => 1, "Tue" => 2, "Wed" => 3, "Thur" => 4, "Fri" => 5,
"Sat" => 6, "Sun" => 7);

my ($time);
my ($dweek, $day, $mon, $year) = split(/-/, $date);
unless ($dweek and $day and $mon and $year) { return undef; }
unless (defined($months{$mon})) { return undef; }
unless (defined($dwk{$dweek})) { return undef; }
use Time::Local;
eval {
$day = int($day); $year = int($year) - 1900;
$time = timelocal(0,0,0,0,$dwk{$dweek}, $day, $months{$mon}, $year);
};
if ($@) { return undef; } # Could return 0 if you want.
return ($time);
}




Quote Reply
Re: [amfreak] How to change date format In reply to
What about leaving the $time-line as it was:

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

timelocal doesn't take a weekday as input, it takes "($sec,$min,$hours,$mday,$mon,$year)" as input and returns a number of seconds.

kellner
Quote Reply
Re: [kellner] How to change date format In reply to
You the Man kellner! Smile works great!
Now onto my next problem....heheheCrazy

here is the final out come.


sub get_date {
# --------------------------------------------------------
# Returns the date in the format "day of the week name-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());
my (@months) = qw!Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec!;
my (@dwk) = qw!Mon Tue Wed Thur Fri Sat Sun!;
($day < 10) and ($day = "0$day");
$year = $year + 1900;

return "$dwk[$dweek]-$day-$months[$mon]-$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 (%dwk) = ("Mon" => 1, "Tue" => 2, "Wed" => 3, "Thur" => 4, "Fri" => 5,
"Sat" => 6, "Sun" => 7);
my ($time);
my ($dweek, $day, $mon, $year) = split(/-/, $date);
unless ($dweek and $day and $mon and $year) { return undef; }
unless (defined($months{$mon})) { return undef; }
unless (defined($dwk{$dweek})) { return undef; }
use Time::Local;
eval {
$day = int($day); $year = int($year) - 1900;
$time = timelocal(0,0,0, $day, $months{$mon}, $year1);
};
if ($@) { return undef; } # Could return 0 if you want.
return ($time);
}

Quote Reply
Re: [amfreak] How to change date format In reply to
great this works, but no matter how often people say this, I'm still the woman, actually. Wink
kellner
Quote Reply
Re: [kellner] How to change date format In reply to
oopppsBlush I had thought of that after I hit the button sorryTongue