简体   繁体   中英

Difference Between Two Date Columns In MySQL

This returns no errors in PHP, but instead of doing the math calculation, it simply returns blank for the final column (all other columns work fine).

SELECT 
    mantis_category_table.name, 
    mantis_bug_history_table.bug_id, 
    FROM_UNIXTIME(mantis_bug_table.date_submitted, "%m-%d-%Y") AS DATE2, 
    FROM_UNIXTIME(min(mantis_bug_history_table.date_modified), "%m-%d-%Y") AS FirstOfdate_modified,
    TO_DAYS(FROM_UNIXTIME(min(mantis_bug_history_table.date_modified), "%m-%d-%Y")) - TO_DAYS(FROM_UNIXTIME(mantis_bug_table.date_submitted, "%m-%d-%Y"))

How can I properly get the difference between the 3rd and 4th items (both dates) in my select statement.

I have already tried DATEDIFF like this:

SELECT DATEDIFF(DATE2,FirstOfdate_modified)

And it doesn't work either.

Thanks.

I assume you have 2 unix timestamps.

The FROM_UNIXTIME() formats them to a string, and subtracting strings isn't a valid operation. (The to_day maybe makes it worse...)

But why don't you subtract the unix timestamps? You will get the result in seconds, but it should work fine.

For example:

SELECT 
    mantis_category_table.name, 
    mantis_bug_history_table.bug_id, 
    FROM_UNIXTIME(mantis_bug_table.date_submitted, "%m-%d-%Y") AS DATE2, 
    FROM_UNIXTIME(min(mantis_bug_history_table.date_modified), "%m-%d-%Y") AS FirstOfdate_modified,
    (min(mantis_bug_history_table.date_modified)- mantis_bug_table.date_submitted)/ 86400 as day_difference.

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