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

I have a MySQL table with a localtime column. Currently it is setup as localtime() but I'd really rather call it as a string ($now_string = localtime).

Either way, how do I determine 24 hours ago from localtime? I want to delete all rows from my database that are 24 hours or older.

Replies are listed 'Best First'.
Re: 24 hours ago localtime
by BrowserUk (Patriarch) on Dec 07, 2005 at 02:31 UTC

    How accurately do you want that "24 hours" to be?

    perl> print scalar localtime;; Wed Dec 7 02:30:59 2005 perl> print scalar localtime( time() - 24*60*60 );; Tue Dec 6 02:31:01 2005

    This works okay, but it may not account for things like summertime, leap days etc.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Oh, I don't care if that 24 hours is sometimes 25 or 26. I just need it to generally round down to 24 hours (with leapyears and other special events, if it doesn't crash it'll still be acceptable).

      Thank you.

Re: 24 hours ago localtime
by TedPride (Priest) on Dec 07, 2005 at 10:11 UTC
    Most people just store the time as a time() value in a number field, which makes things real simple (assuming you don't care about daylight savings and so on). Though if you use the mySQL date/time types, you can also do something like (untested):

    DELETE FROM tablename WHERE UNIX_TIMESTAMP(fieldname) < NOW() - 86400

Re: 24 hours ago localtime
by Anonymous Monk on Dec 07, 2005 at 02:18 UTC
    I know there are MySQL time functions but I'd rather just calculate based on the localtime I inserted in there. (sorry, forgot to post this with the question).