in reply to Re: MySQL Timestamp comparison
in thread MySQL Timestamp comparison

If you use a TIMESTAMP or DATETIME column, you can write a subroutine to convert that into an "seconds since the epoch" number.

This isn't necessary. The UNIX_TIMESTAMP MySQL function takes a datetime (any date type column) as an argument and returns the epoch of the time passed; such as: SELECT UNIX_TIMESTAMP(myDateCol) FROM TABLE;

Also, MySQL has another function called INTERVAL that allows you to do math on the date in the database itself, such as: select * from table where myDateCol < current_date - interval 5 day; will show you everything older than five days ago.

When doing database programming, the hardest thing for me to get over was to try and do everything in the program (in Perl, in C, etc) when the database is (in _most_ cases) going to be able to do it for you a lot more efficiently and a lot faster. Let the database do it's job.. it likes it, really.

Replies are listed 'Best First'.
Re: Re: Re: MySQL Timestamp comparison
by hmerrill (Friar) on Oct 30, 2003 at 15:59 UTC
    The point in creating a subroutine to take in a date/time and return an "seconds since the epoch", is not so much for MySQL specifically, but more for portability - other databases don't have a UNIX_TIMESTAMP() function, and therefore can't give back a date/time column in "seconds since the epoch". So your subroutine can function in a date/time portability role - to convert a date/time, say coming in in YYYY-MM-DD HH:MM:SS format, into a number that is "seconds since the epoch".
      Very true, but in you have already included a MySQL specific function (UNIX_TIMESTAMP) in your above example thus excluding you from any platform independence. Having one function in your script that requires MySQL forces all the rest of your script to require it as well by default (unless you do some sort of modularized set up, but a simple script won't cope.) Therefore your argument of having the subroutine to do the conversion to be cross platform is moot because you are already requiring MySQL.

        How would you make it cross platform. Would it be better to take the time stamping function away from the database and have the scripts create and store the time stamp in an Integer on the database. That way there would be no need for conversion no matter what the database. So what is the benefit of the timestamp function beside having the database handle the function????