Gossamer Forum
Home : Products : DBMan : Customization :

Posting time and date

Quote Reply
Posting time and date
I just read through an old thread of Nightcrawler and JPD trying to get a field to post as
mon-dd-yyyy hh:mm:ss (and possibly am/pm) and well, i ran through it, and tried a few things, none of which are working. It is not important to read incoming user dates, but just displays as a get date...

another question is how can i make it so that the viewall default is sorted by time and date in a descending order?
thanks for any help

Quote Reply
Re: Posting time and date In reply to
If you've gone to the trouble of doing a search for a thread and then refer to it, I'd appreciate it if you'd post the URL of the thread so I don't have to search for it. I'm not even sure which one you were reading.

What have you used that didn't work? And what was the problem you were having?

In Reply To:
how can i make it so that the viewall default is sorted by time and date in a descending order?
You can do it two ways. One is to work with the date and time thing and sort on that field. The other (simpler) is to use a counter for your key field and sort by that field.

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Posting time and date In reply to
doh! sorry... sleepy ... well. it was this thread

http://www.gossamer-threads.com/perl/forum/showthreaded.pl?Cat=&Board=DBCust&Number=1065&page=&view=&sb=&vc=1#Post1065

and i went through it.. and im an idiot and found the problem.. about sorting, if i use a counter and i would assume it would be hidden and would it naturally sort from most recent first?
one more question for you JPD (at least for now =) ) how could i have it cut off after a certain date or number, well i guess what i mean is, if i sort by date in "most-recent-first" fashion, then i would be able to limit the number of posts by date, but could i using the counter ?

thanks again!
for the quick reply too!

how can i make it display "January" as opposed to Jan?


Quote Reply
Re: Posting time and date In reply to
Smile That's okay.

With the counter, you'll need to set so=descend in order to have the most recent records on top.

I'm not sure what you want to do with limiting the number of records returned. It would be simple to have one page full of records, with no links to other pages, if that's what you want.

In Reply To:
how can i make it display "January" as opposed to Jan?
In sub get_date (in the db.cgi file), change the month names in

my (@months) = qw!Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec!;

to the full month names. Be sure that you have a space between them and you have them all on one line.

In sub date_to_unix, you'll need to change

Code:

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);
Just extend the month names within the quotation marks.

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Posting time and date In reply to
thanks for the reply.. turns out i stayed up and got it to do what i wanted (well in the sort business) and consequently missed my first class this morning =(... =)

okay.. in terms of limiting my records, it's a case of having something, such as news or the like and limiting the number of posts viewable ... i saw an archiving mod and may check that out...

for the full month names, i tried that, and had no luck, but that was also when nothing was working, so i;ll give it another shot.. any reason you can think of it not displaying correctly?


EDIT: i just went through and it worked... must been some other errors before.. thanks JPDeni... now just gotta change the am/pm and a couple more things.. =))))))
Quote Reply
Re: Posting time and date In reply to
okay.. i got another question... about the date.. it displays but now as " July-12-2000" how can i get it to display as something like.... July 12, 2000

???? thanks

Quote Reply
Re: Posting time and date In reply to
Sorry you missed your class. [sad]

Regarding your date format, I can give you the code for it, but you mentioned previous thread that included the time as well as the date. The following won't work if you have the time as well as the date in the field.

In sub get_date, change

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

to

return "$months[$mon], $day $year";

(That was easy! Smile)

In sub date_to_unix, change

my ($mon, $day, $year) = split(/-/, $_[0]);

to

my ($mon, $day, $year) = split(/ /, $_[0]);
$day =~ s/,//;




JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Posting time and date In reply to
hmm.. well that's too bad... i tried that last night and figured maybe i forgot something.. but to the same effect... i would imagine there is a way around it.. ?? seems as if date to unix is the catch there... oh well.. how about any ideas on a am/pm solution? i have an idea.. but little/no knowledge of perl and am trying to pick it up cuz it's something to do =)

thanks again

Quote Reply
Re: Posting time and date In reply to
If you'll give me the exact way your date and time is saved to the database, I can tell you how to parse it.

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Posting time and date In reply to
 sub get_time {
# --------------------------------------------------------
# Returns the time in the format "hh-mm-ss".
#
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime(time()+10800);
($sec < 10) and ($sec = "0$sec");
($min < 10) and ($min = "0$min");


return "$hour:$min:$sec";
}

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 ($time) = @_[0];
$time = time()+10800;
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime($time);
my (@months) = qw!January February March April May June July August September October November December!;
($day < 10) and ($day = "0$day");
$year = $year + 1900;
($sec < 10) and ($sec = "0$sec");
($min < 10) and ($min = "0$min");


if ($hour == 12) { $ampm = "pm"}
elsif ($hour == 0) {$hour = 12; $ampm = "am"}
elsif ($hour < 12) {$ampm = "am"}
else {$hour -= 12; $ampm = "pm"}

return "$months[$mon] $day, $year $hour:$min:$sec $ampm";
}

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_and_time) = $_[0];
my (%months) = ("January" => 0, "February" => 1, "March" => 2, "April" => 3, "May" => 4, "June" => 5,
"July" => 6, "August" => 7, "September" => 8, "October" => 9, "November" => 10,"December" => 11);

my ($mon, $day, $year, $time, $ampm) = split(/ /, $date);
$day =~ s/,//;
my ($hour, $min, $sec) = split(/:/, $time);
if (($hour == 12) && ($ampm eq "am")) {$hour == 0; }
elsif (($hour<12) && ($ampm eq "pm")) {$hour +=12; }
unless ($mon and $day and $year) { return undef; }
unless (defined($months{$mon})) { return undef; }

use Time::Local;
eval {
$sec = int($sec);
$min = int($min);
$hour = int($hour);
$day = int($day);
$year = int($year) - 1900;
$time = timelocal($sec,$min,$hour,$day, $months{$mon}, $year);
};
if ($@) { return undef; } # Could return 0 if you want.
return ($time);
}
Quote Reply
Re: Posting time and date In reply to
What I was hoping for was that you would give me an example from your database, like you did with "July-12-2000" a couple of posts up. Let's see if I can work it out from the code, though.

In sub get_date, change

return "$months[$mon]-$day-$year $hour:$min:$sec";

to

if ($hour == 12) { $ampm = "p.m." }
elsif ($hour == 0) { $hour = 12; $ampm = "a.m."; }
elsif ($hour <12) { $ampm = "a.m." }
else { $hour -=12; $ampm = "p.m." }
return "$months[$mon], $day $year $hour:$min:$sec $ampm";


In sub date_to_unix, change

my ($date, $time) = split(/ /, $_[0]);
my ($mon, $day, $year) = split(/-/, $date);


to

my ($mon, $day, $year, $time, $ampm) = split(/ /, $date);
$day =~ s/,//;
my ($hour, $min, $sec) = split(/:/, $time);
if (($hour == 12) && ($ampm eq "a.m.")) { $hour == 0; }
elsif (($hour<12) && ($ampm eq "p.m.")) { $hour +=12; }


I think that should give you something like July 12, 2000 4:30:23 p.m.. If not, let me know and I'll see what I can figure out.

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Posting time and date In reply to
it does work... But only one thing... i get an invalid date format when trying to post... you did say that the date and time together would not work in the "July 12, 2000 1:20pm" mode... but on that last bit of code.. it seems as if it may?? am i mistaken?

Quote Reply
Re: Posting time and date In reply to
It should work with

July 12, 2000 1:20 p.m.

but it needs to be exactly in that format.

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Posting time and date In reply to
Okay.. i edited the above post and now has the code exactly how i have it.. and it returns invalid date format WHEN i try to add... it does display correctly tho... some parameter still is reporting it as not the right format when i try to add.. any idea what that might be?

okay... i would like the display of "July 12, 2000 12:20 pm" without the seconds... and just like that... what am i doing wrong??

thanks for your time JPDeni... really.


okay.. it will post "July-12-2000 12:27am" but it will not do "July 12 2000 12:27am" or even "July.12.2000 12:27am" and im not sure why....