Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a database full of timestamps that look like 2005-02-08 00:51:22 . How can I check to see if the timestamp was within 24 hours?

Actually I am going through a mysql table where I have to check every cat_id to see if there was an entry within the past 24 hours.

Replies are listed 'Best First'.
Re: Detecting 1 day ago
by japhy (Canon) on Feb 25, 2006 at 03:51 UTC
    In mysql, you can use SUB_DATE and INTERVAL, as demonstrated here.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      I could do that, but I'd rather a perlish way to do it.

      Thanks!

        No, you don't. You want a MySQLish way of doing it because you're using MySQL and it'll be 10x faster to have MySQL figure it out.

        Otherwise, look up DateTime, Date::Calc, or Date::Manip. All three have functionality to do what you're looking for.


        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
        I could do [the sensible thing], but I'd rather a perlish way to do it.

        If you're dead-set against doing the comparison in SQL, you could use DateTime::Format::MySQL to convert the dates to DateTime objects, ->add(days=>1), and compare to DateTime->now() using DateTime->compare(). This will not perform as well (in terms of execution time) as doing it in SQL, however. It would be one thing if you were occasionally checking a date this way, or if you were doing more complex things with the dates and needed the extra functionality DateTime provides, but if all you're doing is looping through all the rows of a table and getting a list of the ones where a certain field is less than a day ago, throwing a WHERE clause into the query seems like a much better solution to me. Really, it does. Even if you're mostly not directly interacting with the DB at the SQL level (e.g., if you're using an abstraction layer like Class::DBI), you still could use a single SQL query just to build a list of record IDs that you want and then map those over to your abstraction-layer objects.

Re: Detecting 1 day ago
by Random_Walk (Prior) on Feb 25, 2006 at 15:03 UTC

    If you insist on a Perl way and as it looks, your date is in ISO 8601 format (YYYY-MM-DD HH:MM:SS) you can dervive the date stamp for 24 hours ago and do a string comparison

    #!/usr/bin/perl use strict; use warnings; use POSIX; my $one_day = (24 * 60 * 60); while (<DATA>) { chomp; my $day_ago = strftime "%Y-%m-%d %H:%M:%S", localtime (time - $on +e_day); print "$_ is ", ( $_ gt $day_ago ? "recent" : "old" ) , $/; } __DATA__ 2005-23-02 02:02:02 2006-02-25 15:52:35
    I love ISO 8601 dates for this feature.

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Re: Detecting 1 day ago
by EvanCarroll (Chaplain) on Feb 25, 2006 at 04:30 UTC
    I agree have mysql do it! Too bad you're using an inferior database otherwise it would be as simple as datecol < now() + "1 day ago"::interval


    Evan Carroll
    www.EvanCarroll.com
      I'll grant the MySQL is arguably less capable than some older established servers, but it's respectably simple for the task at hand:
      select count(*) from mytable where TO_DAYS(datecol) > TO_DAYS(NOW()) - + 1