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

Hi,

I am trying to select all records from a table that are over (for instance) 48 hours old. The date/time field was populated with a UNIX_TIMESTAMP() call.

The problem I have is that Perl's time() function and MySQL's UNIX_TIMESTAMP() function seem to have completely different ideas about what time it is.

Can anybody shed any light on this, maybe I can use MySQL's time functions in my SELECT statement?

Cheers

Stew
  • Comment on MySQL, UNIX_TIMESTAMP() and Perl's time()

Replies are listed 'Best First'.
Re: MySQL, UNIX_TIMESTAMP() and Perl's time()
by hardburn (Abbot) on May 16, 2003 at 14:53 UTC

    Can anybody shed any light on this, maybe I can use MySQL's time functions in my SELECT statement?

    Yes, that's exactly what you should do. Something like:

    SELECT col1, col2 FROM table WHERE time > (TIMESTAMP() - (48 * 60 * 60))

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: MySQL, UNIX_TIMESTAMP() and Perl's time()
by gmax (Abbot) on May 16, 2003 at 15:21 UTC
    Perl's time() function and MySQL's UNIX_TIMESTAMP() function seem to have completely different ideas about what time it is

    Actually, they don't. The epoch from where time is calculated is January 1, 1970 for both MySQL and Perl, unless you happen to use a Mac OS, where the epoch is January 1, 1904.

    $ perl -e 'print time(), "\n";' ; mysql -e "select unix_timestamp()" 1053097150 +------------------+ | unix_timestamp() | +------------------+ | 1053097150 | +------------------+

    See also Perl's time and MySQL's timestamp for more about this issue.

    _ _ _ _ (_|| | |(_|>< _|
Re: MySQL, UNIX_TIMESTAMP() and Perl's time()
by Limbic~Region (Chancellor) on May 16, 2003 at 14:53 UTC
    stew,
    I don't know anything about much, but I heard one of the gods saying that MySQL uses localtime timestamps.

    Hope that helps if you are getting more/less than you expect,
    L~R

Re: MySQL, UNIX_TIMESTAMP() and Perl's time()
by zby (Vicar) on May 16, 2003 at 15:00 UTC
    > Can anybody shed any light on this, maybe I can use MySQL's
    > time functions in my SELECT statement?

    Why not?

    SELECT * FROM YOURTABLE WHERE YOURCOL + 48 * 3600 > UNIX_TIMESTAMP();
    The code is untested.
      That will work - but it will have to do a calculation for every value of YOURCOL. You may want to change it around to:
      SELECT * FROM YOURTABLE WHERE YOURCOL < UNIX_TIMESTAMP() - 24 * 3600;

      That way the db only does one calculation and can then compare it to the values of YOURCOL and even still use an index if there is one.

      my @a=qw(random brilliant braindead); print $a[rand(@a)];
Re: MySQL, UNIX_TIMESTAMP() and Perl's time()
by stew (Scribe) on May 16, 2003 at 14:59 UTC
    Perlmonks to the rescue again!!

    Thanks chaps....