PDA

View Full Version : Difference of Dates


macchisp
08-01-2006, 02:55 PM
I am trying to subtract the current date from a date I have stored in a database. I am trying make a post that says "2 days ago," similar to RSS feeds. I am using Date::Manip and the format for the current date is:
my $currentDate = &UnixDate(&ParseDate("today"),"%A %B %d %Y %I:%M:%S %p");

Will the date I'm comparing have to be in the same format?

Does any one know any good information out there on how to get the difference of two dates?

Thanks!

FishMonger
08-01-2006, 05:43 PM
Date::Mainip is a little overkill for formating the date. The strftime function from the POSIX module would be much faster. However, Date::Manip does have functions for doing date calculations. Have you read its documentation?

There are a couple other modules that you might want to look at.
http://search.cpan.org/~stbey/Date-Calc-5.4/Calc.pod
http://search.cpan.org/~tobix/OO-DateTime-0.01/Time.pm

What type of database are you using? Is it an actual database or is it a flat csv file that you parse. Most, if not all, databases have functions for doing date/time calculations; have you looked into those functions?

macchisp
08-01-2006, 07:56 PM
I read its documentation and I don't understand the DateCalc method. I don't understand why it takes into account business days and normal days and I don't understand Deltas. Let me explain what I am trying to do:

I have a script that once it runs, it inserts a record into a CSV database. One of the fields (date field), is dynamically created using the Date::Manip line in the original post. Basically, I'm inserting the time stamp the post was created.

I have this other script that reads the database and lets the user view it. On these posts, I want to display how long ago the post was made to the current time. So for example, goto http://my.yahoo.com/index.html, and I would like the part that says "x minutes/hours ago" to appear on my post as well as everything else I'm currenting displaying.

So what I'm trying to do is read the CSV database, write a query to get the time stamp for each post, and subtract it from the current date/time.

I haven't gotten the difference between the dates part but I am still continuing to look. I haven't looked into much else because I don't have access to install the modules on the server.

Thanks for helping me out,
macchisp

FishMonger
08-01-2006, 08:11 PM
The best method is to store the date as a true timestamp, such as the number of secends since epoch, instead of a formatted string. Are you willing to make that change, or must it do the multiple conversions needlessly?

macchisp
08-01-2006, 09:44 PM
Thanks Fish...

I stored it as the number of seconds from epoch and then took today in seconds from epoch and subtracted those numbers and then converted the difference of seconds to minutes, days, and so forth. Thanks a lot for the idea. I never would have done it with out you.

FishMonger
08-01-2006, 10:44 PM
Glad I was able to help. :)

macchisp
01-09-2007, 05:19 PM
Hello,

Fish helped me last time with the original difference of dates, but since we are in a new year, I am having trouble getting the difference of dates from today to any day last year.

So how would I get the difference of two dates from January 9 to December 1? It's 40 days by my count, but I cannot get it in perl.

Does anyone have any ideas?

macchisp
01-09-2007, 05:25 PM
I got it by doing some simple math...just divide the seconds by 86400 to get the difference in days

Sorry:)