in reply to Detecting 1 day ago

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

Replies are listed 'Best First'.
Re^2: Detecting 1 day ago
by Anonymous Monk on Feb 25, 2006 at 03:59 UTC
    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?
        Okay, the MySQL way would be something like the following?
        SELECT * FROM messages -> WHERE DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= message_data;
        I generally do this in a three stage format, so if the above is true, I know there's a better way of doing this.
        my $data = qq(SELECT * FROM messages..); my $sth = $dbh->prepare($data); $sth->execute();
        Instead of caring what results came back, I only need to check to see that at least SOMETHING came back in the last day. How would I do that without having to do an sth->fetch?

        Sorry, new to mysql.

      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.