Calculate time difference from MySQL time - mysql

I am using the following call in my query to calculate the amount of time between now and a timestamp:
(NOW() - bu.banusr_expire)
bu.banusr_expire is a TIMESTAMP field.
I'm a little confused about the number it is returning.
ex; it returns -928 when there is about a 9.5 minute difference.
This makes me think that -928 = -9mins and 28 seconds(or 15 seconds. This set of digits seems to go from 0-99), but that seems completely wrong.
My question is, how can this value be converted to minutes?

If you can be confident that the difference between the two times will always be less than 839 hours, then you can use TIMEDIFF().

(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(bu.banusr_expire)) / 60
should give you the number of minutes ;)

use unix timestamp
select UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(fieldname)
this will give you the diff in seconds you will have to divide by 60 for minutes

Related

How to sum time interval stored as string in MySQL like 5 days 10:20:00

I need to write a select query to sum the time interval from MySQL table where the time interval is stored as text and in the format similar to following
10 days 3:28:31
In the PostgreSQL query we can simply use ::interval and it converts above to interval and we can use Sum method over it in PostgreSQL query but I am unable to do the same in MySQL. Any help would be appreciated.
MySQL does not have an interval data type. It does use the interval keyword -- which is a bit confusing. But that is a syntactic element, rather than a data type.
One thing you can do is use the time data type. This supports times up to 838 hours -- or about 35 days. That is enough for many purposes.
Otherwise, the recommendation is to use a single time unit and to store the value as a numeric quantity. For instance, "5 days 10:20:00" would be:
5.43055555556 days (5 + 10 / 24 + 20 / (24*60))
130.333333333 hours
7820 minutes

mysql timestampdiff always returning null?

I am using version 14.4 of mysql and I am trying to execute the following query:
SELECT TIMESTAMPDIFF(MINUTE, MINUTE(NOW()), NOW())
This should return a timestamp that is on the current hour, but it's always returning null. TIMESTAMPADD works just fine, I am only having trouble with this function. I have looked for answers to this problem through google and mysql documentation but I couldn't find anything.
Does anyone have any an idea on what's going wrong?
My full goal is a query that returns how many minutes are left from now until 5 of the next hour. For example. If its 1:30, our target time is 1:55, so the query would return 25
If you want the hour
SELECT CURTIME()
That query you posted is absurd,or be more clear about what you want.
I believe you're trying to floor the current UNIX timestamp to the current hour? If so, why not be explicit about it?
SELECT UNIX_TIMESTAMP() - UNIX_TIMESTAMP() % 3600;
Based on your edits, you can also calculate the difference to the 5 minute mark preceding the next hour as follows:
SELECT MINUTE( TIMEDIFF(
NOW(),
FROM_UNIXTIME( UNIX_TIMESTAMP() - UNIX_TIMESTAMP() % 3600 + 3600 - 5*60 )
) );
This is just a proof-of-concept query; I haven't yet accounted for rounding the current time to the minute (because I don't know if you want to go up or down), nor handled the edge case where the current time is within 5 minutes of the next hour already (e.g. 1:59).

Mysql Time Arithmetic - Time only

This should be so easy... but is driving me mad.
UPDATE time SET time = (time - interval 130 minute) WHERE stuff=whatever;
Time is a time column only, i.e. 09:00:00.
Assuming that you would like to subtract 130 minutes from the current time, you can use addtime, like this:
UPDATE time SET time = addtime(time, '-02:10') where stuff=whatever
130 minutes is 2 hours and 10 minutes, hence the -02:10 constant.
Here is a quick demo on sqlfiddle.
Change - to , and it will work. The correct Query is:
UPDATE time SET time = (time, interval 130 minute) where stuff=whatever
If time is a datetime or a timestmap, you must use a date_sub funktion
SELECT date_sub(time, interval 130 minute) FROM ....
Otherwise you can also convert your time with UNIX_TIMESTAMP, sub it and convert with FROM_TIMESTAMP into a mysql timestamp back
There is a DATE_SUB method that works like the DATE_ADD method you are looking for.
DATE_SUB(NOW(),INTERVAL 130 MINUTE)
Check this link for more information:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-sub

Convert timestamp to X days X hours X minutes ago

I've created a stored procedure in MySQL Server 5.1.
How can I convert a timestamp to a string that represents the days, hours, and minutes difference between that timestamp and now?
For example, if the timestamp is 5 hours and 3 minutes ago I'll get '5 hours 3 minutes ago'.
select date_format(timediff(current_timestamp,last_timestamp),
'%k hours, %i minutes, %s seconds ago');
If you want more luxury you can do something like:
select concat
(
if(date_format(timediff(ts1,ts2)'%k')='0'
,'',date_format(timediff(ts1,ts2)'%k hours'),
if(date_format(timediff(ts1,ts2)'%i')='0'
,'',date_format(timediff(ts1,ts2)'%k minutes'),
if(date_format(timediff(ts1,ts2)'%s')='0'
,'',date_format(timediff(ts1,ts2)'%k seconds')
)
Plus a few extra spaces here and there.
If one of the timestamps is null naturally the result will also be null, you'll have to make sure it is not, or use ifnull(`timestamp`,now()) to fix that.
See: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff
Have a look at the MySQL reference page for date and time functions at
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime
Edit: Since I assume you are using Unix timestamps, the way to go is
FROM_UNIXTIME(timestamp, format)

How do I subtract using SQL in MYSQL between two date time values and retrieve the result in minutes or second?

I want to subtract between two date time values using SQL in MySQL such that I get the interval in minutes or seconds. Any ideas? I want to run a SQL query that retrieves uses from a database who have logged in like 10 minutes from the time.
There are functions TIMEDIFF(expr1,expr2), which returns the value of expr1-expr2, and TIME_TO_SEC(expr3), which expresses expr3 in seconds.
Note that expr1 and expr2 are datetime values, and expr3 is a time value only.
Check this link for more info.
TIMESTAMPDIFF is like TIMEDIFF which Matthew states, except it returns the difference of the two datetimes in whatever units you desire (seconds, minutes, hours, etc).
For example,
SELECT TIMESTAMPDIFF(MINUTE,LogOutTime,LogInTime) AS TimeLoggedIn
FROM LogTable
Would return the number of minutes the user was logged in (assuming you stored this kind of thing in a table like that).
I would do it like this - fetch where last activity is within 10 mins of now
SELECT * FROM table WHERE last_activity >= DATE_SUB(NOW(), INTERVAL 10 MINUTE)
SELECT TIMESTAMPDIFF(MINUTE,LogOutTime,LogInTime) AS TimeLoggedIn
FROM LogTable
This example shall ruin the time if its used by using millitary time. So for calculating millitairy time I do not recommend it Because it outputs negative values.
You can try and cast them to Unix Time stamp, and take the difference.