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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Re: [davorg] Day of Week

 



allanonline
Novice

Nov 30, 2003, 4:42 AM

Post #1 of 5 (453 views)
Re: [davorg] Day of Week Can't Post

Hi Dave,

I've been going through the posts and haven't seen one that is similar to the problem I'm having. I have however noticed that you seem to be the one answering most of the questions dealing with dates.

Here is what I need, and hope you can help me with.

I have an inventory of vehicles on an auto website, one of the fields is called (date), this field is entered manually in the format YYYYMMDD. I want to give the user the option of selected "Most Recent Additions", and the results display only those records that were added in the last 7 days

I'm sure this is possible, just not sure how to code it. I'm thinking something along the lines of

If date - system date <= 7 show results

Can you help me out with this?

Thanks

Allan
Unsure


KevinR
Veteran


Nov 30, 2003, 11:03 AM

Post #2 of 5 (450 views)
Re: [allanonline] Day of Week [In reply to] Can't Post

The yyyymmdd format is fine for humans but what you should do if you are not already, is also store the raw date stamp that the server will generate for you when you use the time() function. This way you can easily perform any task associated with the date, even comparing dates down to the second. You might also be able to use the modified date of the actual file instead of the date entered to do the comparison:


while ( <FILE> ) {#assumes the file is already open
push (@recent,$_) if (-M $_ <= 7);
}

foreach (@recent) {
do something with the list
}

or wait for Dave to provide a solution Wink
-------------------------------------------------


davorg
Thaumaturge / Moderator

Dec 1, 2003, 3:54 AM

Post #3 of 5 (442 views)
Re: [allanonline] Day of Week [In reply to] Can't Post

(By the way, I've moved this to be a separate thread. It's never a good idea to ask a new question in an existing thread.)

The easiest way to deal with dates in Perl is to use the same representation that Perl does. That is to convert dates and times to the number of seconds since some arbitrary point. For most systems that point is defined as 1st Jan, 1970 (this system can therefore be flaky for dates before that).

You can always get the current in that format using the "time" function.

Code
my $now = time;

This is currently a number that is just over 1e9.

Now all you need to do is to convert a date in YYYYMMDD to one of these "epoch seconds" values. I suggest you write a function for this and use the standard Time::Local module.

Code
use Time::Local; 
sub date2epoch {
my $date = shift;

my ($y, $m, $d) = $date =~ /(\d\d\d\d)(\d\d)(\d\d)/;

# check the match worked
unless ($y && $m && $d) {
die "invalid date $date\n";
}

# assume midnight
# see perldoc -f Time::Local for parameter definitions
my $epoch = timelocal(0, 0, 0, $d, $m - 1, $y - 1900);

return $epoch;
}


You can then compare dates to the current time to see if they appeared in the previous week (i.e. 7 * 24 * 60 * 60 seconds).

Code
my $date = '20031128'; # or whatever 

use const ONE_WEEK => (7 * 24 * 60 * 60);
if ($now - date2epoch($date) < ONE_WEEK) {
print "$date is in the last week\n";
}


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Recall
Novice

Dec 1, 2003, 4:43 PM

Post #4 of 5 (436 views)
Re: [davorg] Day of Week [In reply to] Can't Post

use const => use constant ?


davorg
Thaumaturge / Moderator

Dec 2, 2003, 1:48 AM

Post #5 of 5 (433 views)
Re: [Recall] Day of Week [In reply to] Can't Post


In Reply To
use const => use constant ?


Er... yeah... sorry about that. use constant

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


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

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