CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Advanced:
Playing with dates



Nov 1, 2001, 9:33 AM

Post #1 of 2 (1269 views)
Playing with dates Can't Post


I have a database with many records. I would like to display a "New" image next to the record if that recrods was added within 7 days.

The current format of the dates in the database are e.g. 11/01/2001.
I can access the date field by doing something like $fields{'date'}.

Thanks for your help


Nov 2, 2001, 2:47 AM

Post #2 of 2 (1259 views)
Re: Playing with dates [In reply to] Can't Post

I don't know what kind of database U use.

Is it a flat-text-file, or MySQL, and what do you mean with 'an image inserted' in the database. Is that a filename or what else?

About the date.
In the perldoc there are many answers for your question.
However, you stored your date in a very difficult format to compare it. You need to take care or leapyears, long and short months (maybe DST times) etc to determine what is actually 7 days ago.

In perl you have the time() function. They return the current time in seconds from epoch. Just store that value in your database. That value can be passed through localtime() or gmtime() to get a time string.


# constants for localtime() array 
use constant SEC => 0;
use constant MIN => 1;
use constant HOUR => 2;
use constant MDAY => 3;
use constant MON => 4;
use constant YEAR => 5;
use constant WDAY =>6;
use constant YDAY => 7;
use constant DST => 8;

my $Time = time(); # current time

my $SevenDays = (60 * 60 * 24 * 7); # in seconds
my $Earlier = $Time - $SevenDays;

my $TimeStr = localtime($Earlier);
print "Seven days earlier: $TimeStr\n";

my @Time = localtime($Earlier);
print "$Time[YEAR]/$Time[MDAY]/$Time[MONTH]\n";


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives