Jump to content
MakeWebGames

A wise word is needed...


Recommended Posts

Let's say I have a DATETIME field and i need to calculate the interval between that field and current time.

Using php is not an option, as it needs to be done entirely on top of the DB.

I've reached this result:

 

mysql> SELECT SEC_TO_TIME(TIME_TO_SEC(TIMEDIFF(NOW(), '2011-07-22 23:59:59')));
+------------------------------------------------------------------+
| SEC_TO_TIME(TIME_TO_SEC(TIMEDIFF(NOW(), '2011-07-22 23:59:59'))) |
+------------------------------------------------------------------+
| 34:54:36                                                         |
+------------------------------------------------------------------+
1 row in set (0.00 sec)

 

Still as you can see hours go up to 34, and I need to effectivelly trim to display it in a format quite near the following:

 

1 day 10:54:36 

 

Any help or some pointers would be appreciated.

Link to comment
Share on other sites

  • 4 weeks later...
DROP FUNCTION IF EXISTS time_since;

CREATE FUNCTION time_since( s INT )
      RETURNS CHAR(20) DETERMINISTIC
      RETURN CONCAT
      (
          FLOOR((UNIX_TIMESTAMP() - s) / 86400),
          ' day',
          IF(FLOOR((UNIX_TIMESTAMP() - s) / 86400) = 1, '', 's'),
          ' ',
          SEC_TO_TIME((UNIX_TIMESTAMP() - s) % 86400)
      );

SELECT time_since( UNIX_TIMESTAMP('2011-07-22 23:59:59') );


25 days 18:21:09
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...