Reply
MySQL: find difference between two datetimes
Old 08-21-2006, 04:39 AM MySQL: find difference between two datetimes
mtishetsky's Avatar
King Spam Talker

Posts: 1,117
Name: Mike
Location: Mataro, Spain
I am stuck with the problem of getting the difference of two datetimes (or they may be timestamps) to show how long ago the record was updated. Let's say my table has field `modified` which is datetime and means the date and time the record was last modified. I try to do it in a way like this:
Code:
select *, date_format(now() - modified, "%H:%M:%S") as mod from table
But this returns records with empty `mod` field. I feel taht there is a way to do it in a single query, without post-processing dates in php, but I cannot understand what am I doing wrong.
__________________
Free Mobile Phone Themes

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
When You Register, These Ads Go Away!
Old 08-21-2006, 04:54 AM Re: MySQL: find difference between two datetimes
mtishetsky's Avatar
King Spam Talker

Posts: 1,117
Name: Mike
Location: Mataro, Spain
OK, seems like I found some solution, but it seems to work wrong
Code:
select *, now() as now, from_unixtime(unix_timestamp(now()) - unix_timestamp(accessed), "%H:%i:%S") as ftd from search
returns
Code:
[16] => stdClass Object
        (
            [id] => 52359
            [query] => AWInstall
            [counter] => 0
            [cached] => 2006-08-17 18:09:35
            [accessed] => 2006-08-21 14:20:37
            [now] => 2006-08-21 14:45:47
            [ftd] => 06:25:10
        )
As we can see, the difference between now() and accessed is 25 minutes and 10 seconds, where does it get those 6 extra hours?
__________________
Free Mobile Phone Themes

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 08-21-2006, 04:58 AM Re: MySQL: find difference between two datetimes
mtishetsky's Avatar
King Spam Talker

Posts: 1,117
Name: Mike
Location: Mataro, Spain
Yep. Found the bug. The UNIX epoch begins at 6:00am on 1st January 1970. That 6 hours are added to my `ftd`, so I just add 64800 seconds and get the right formatted date.
__________________
Free Mobile Phone Themes

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Reply     « Reply to MySQL: find difference between two datetimes
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.11666 seconds with 12 queries