in reply to Re: Detecting 1 day ago
in thread Detecting 1 day ago

I could do that, but I'd rather a perlish way to do it.

Thanks!

Replies are listed 'Best First'.
Re^3: Detecting 1 day ago
by dragonchild (Archbishop) on Feb 25, 2006 at 04:09 UTC
    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.

        my $sth = $dbh->prepare( q{SELECT 1 FROM messages WHERE DATE_SUB(CURDA +TE(),INTERVAL 1 DAY) <= message_data LIMIT 1; } ); $sth->execute(); print "We got it" if $sth->rows();
        See exists clause if mysql supports it, and if you can make it better fits your needs..


        Evan Carroll
        www.EvanCarroll.com
Re: Detecting 1 day ago
by jonadab (Parson) on Feb 25, 2006 at 14:01 UTC
    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.