简体   繁体   中英

Query select between dates unix timestamp and date formats

I'm trying to select all dates between 2 dates. In the database I have records in unix timestamp format while my date_ini and date_final variables are in date format (00-00-00 00:00:00)

    $msgs = mysql_query("SELECT date_time, from_user_name, message 
                         FROM message_log
                         WHERE date_time BETWEEN '$log_ini' AND '$log_fim' 
                         AND (from_user_name = '$usuario' OR from_user_name = '$atendente')")or die(mysql_error());

It simple returns null. I'm sure there are records that match the select. I have checked also the date_default_timezone_set and set it to same time to the server. I know this is a silly one, but I'm getting really frustrated! It's been hours I'm trying everything, FROM_UNIXTIME function, no coman, coman, <> operators, strotime in the variables before, and nothing works... Can someone point out where is the mistake? Thanks a lot!

EDIT:

I got the problem, it's the date_time fiels, which is actually not really unixtimestamp, but unix time + 3 digits. SORRY, was my mistake and the answer was good. I'm open another question to find now how to deal with this fiel in this formt. Thanks a lot!

Ok, your date_time field is unix_timestamp? That's just an integer, and you're forcing MySQL to do a 'between' operation on that - MySQL has no native "unix timestamp" format, it's just a convenience function to convert integer<->datetime

For the where clause to work, you'd need

WHERE FROM_UNIXTIME(date_time) BETWEEN '...' AND '...'
... or ...
WHERE date_time BETWEEN UNIX_TIMESTAMP(...) AND UNIX_TIMESTAMP(...)

so you're properly comparing apples to apples, and not the apples to oranges you are.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM