Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



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,226
Name: Mike
Location: Mataro, Spain
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

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

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
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?
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

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,226
Name: Mike
Location: Mataro, Spain
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

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 Search this Thread
Search this Thread:

Advanced Search

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

BB 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.11626 seconds with 12 queries