mysql timestampdiff always returning null? - mysql

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).

Related

Mysql select minutes from future date time - now()

I have a column in my database which holds an expire time being inserted with NOW() + INTERVAL 30 MINUTE;
I need to be able to pull out minutes remaining from that column. I have no idea where to start.
Thanks in advance.
SELECT TIMESTAMPDIFF(MINUTE,NOW(),column1) AS minutes_remaining FROM table1;
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_timestampdiff
Note that the order of the arguments is extremely counterintuitive... to get a positive number as a result, the larger (later) datetime value is the last argument. If the second argument is >= the third, the result will be <= 0.

Fetch mysql data of timstamp depending on time given

I have a column with timestamp, contain example value "2014-04-16 18:00:00","2014-04-17 18:00:00"....
Now, if I will call a page before "2014-04-17 12:00:00" I need this value-"2014-04-16 18:00:00"
And if I call my page after "2014-04-17 12:00:00" I need this value "2014-04-17 18:00:00".
I think my question is very complicated to understand, having complications in date & times, please check date & time properly.
I want to fetch this data from DB in mysql, The page I was saying is that where I'm going to add your mysql query.
Thanx in advance
Generalising what your asking for a bit the following will return dates from the previous day if it's before noon and dates from today if it's after noon:
SELECT date_column
FROM yourTable
WHERE DATE(DATE_SUB(NOW(), INTERVAL 12 HOUR)) = DATE(date_column);
Edit:
The WHERE clause First gets the current time (NOW()) and subtracts 12 hours. This wont affect the date unless the time is before 12. This means DATE_SUB(NOW(), INTERVAL 12 HOUR) gives us today if it's after noon and yesterday if it's before.
We then check if the date_column matches the date we've created (using the DATE function so that the time is ignored).
Adding some rows to the SELECT may help you see how these dates are built up.

Get the time from the last post in seconds

I am trying to write up a query that will give me the time since the last post in seconds, something along the lines of
SELECT (NOW() - mydatetime) as val1 FROM posts ORDER BY mydatetime DESC LIMIT 1
How do I get val1 in seconds?
Since you are only interested in seconds, you can simply subtract the two dates as timestamps:
SELECT UNIX_TIMESTAMP() - UNIX_TIMESTAMP(mydatetime) FROM ...
Note: MySQL has a set of Date Time Functions, I encourage you to browse through them.
TIMESTAMPDIFF(SECOND, mydatetime, NOW())

MySQL select rows that are exactly 7 days old FROM TIMESTAMP

first of all, I know that my question is very similar to that one:
MySQL select rows from exactly 7 days ago
the difference is that my dates are stored in the database as a timestamp.
I know that I can use FROM_UNIXTIME to get the date from the timestamp, the thing is, in another answer I read that was very resource consuming (because the timestamp field has to be converted to date in all the records before comparing).
DATE(from_unixtime(timestamp)) = CURRENT_DATE()
Is there any optimized way to do this?
Turn it around: calculate the unix timestamp of the target date first and use that.
WHERE timestamp = UNIX_TIMESTAMP(NOW() - INTERVAL 7 DAY)
MySQL should calculate that value once and use it all the time (needs testing though). If it doesn't, use a variable or code.

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.