mysql query current time - interval not working - mysql

I am trying to use a query that previously worked but the interval part no longer seems to work it just pulls up all my data
query is
SELECT * FROM database.table WHERE date = CURDATE() AND time > curtime()-INTERVAL 20 second
any idea's where i am going wrong?
EDIT, i should add im running mysql V 5.6.37 complied for linux
2nd Edit: I have added a photo of my table my desired result is to have all data rows from the previous 20 seconds of the Current time e.g. if the current time is 14:11:40 pull all data between 14:11:40 and 14:11:20 I have trucated data so i now only have today's date but date wasnt and issue just time even when i remove the data part it still doesn't work
Many thanksTable

Try this :
SELECT *
FROM bluserve_wp635.OSM
WHERE ADDTIME(CONVERT(date, DATETIME), time) > DATE_ADD(NOW(), INTERVAL 20 SECOND)

Now Solved stupidly i didnt take into account the server the mysql database is hosted on and it had resolved itself back to local time CAl, US rather than GB thank you all for your help

Related

Is PHPmyadmin current date based on my computer's localdate?

I'm doing some testing for my system in selecting data between two dates.
so I tried changing my computer's localdate to like year 2020 and run my system, so I'm expecting my CURRENT_DATE is May 10, 2020.
and I wont be getting any rows from my query because all of my data is year 2018
But after I use my query of cur_date() its still selecting those 2018 rows.
so I thought maybe my Phpmyadmin has its own cur_date().
I'm doing this test for my system will be use for the next couple of years. so I want to try and test my queries if today is already 2025 or something.
I thought maybe my Phpmyadmin has its own cur_date().
PhpMyAdmin has nothing to do with it. Your local computer also has nothing to do with it.
When you put CURDATE() in a query, that's part of the query. It's just text, like the SELECT part or the FROM part.
That means it's evaluated by the MySQL server. Just like the data of your rows is retrieved from the server, not from PhpMyAdmin or your local computer.
So the date returned will be that of the MySQL server.
so I want to try and test my queries if today is already 2025 or something.
The way to do this is to take out the expression CURDATE(), and replace it with the "fake" date you wish to use instead.
Something like:
SELECT * FROM `TheTable` WHERE `TheDate` > '2025-01-01';

MySql returns wrong time from table data

I have data in a table, and one of the columns is DATETIME.
select time from tbdt where unix_timestamp(time) > unix_timestamp(now()) order by time asc limit 1
now NodeJS prints wrong time in console.log() like,
actual datetime is 2018-12-16 15:00:00 in db table..
but mysql returns 2018-12-16T09:30:00.000Z
which is 5 hours 30 minutes difference and my time zone is +5:30 (IST)
I don't exactly know where it goes wrong, either in MySql or in Node Js
Need to use convert_tz function in MySql.
SELECT CONVERT_TZ('2004-01-01 12:00:00','+00:00','+5:30');
It has the following signature:
CONVERT_TZ(dt,from_tz,to_tz)
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_convert-tz
You should check the server time on which your MySQL is running as it will pick the time from the server on which it is hosted.
This is being done by the MySQL, not by NodeJS and you can verify the same by directly running your above query into the database by console or SQL developer tool.

Handle a different timezone in mysql

Long story short, I'm working on a server that has a different time zone to my local area, although the output should be relevant to this local area alone. I wrote a query to see if some record had been stored for 3 or more hours, and that works a treat, however, I'm trying to find a way to re-write it so that it can convert the time to this time zone, prior to comparing.
With my most recent attempt, it looks like it compares the time prior to converting the time, I was just wondering if there’s a way of doing it through mysql, I should probably also mention that I’m currently limited to using version 5.096. Below you can see what I’ve written so far, I only noticed there was a timezone issue once I actually uploaded a file onto my FTP and tested it through that.
If I’m doing something stupidly wrong, please tell me what I’m doing wrong exactly, and if you could provide a solution, it would be much appreciated! Thanks!
SELECT * FROM
(
SELECT *, IF(HOUR(TIMEDIFF(CONVERT_TZ(current_time, '-07:00', '+00:00'), Time)) >= 3, 1, 0) = 1 AS timing
FROM callLogs
WHERE Inqueue = '1'
ORDER BY Time, Inqueue ASC
) AS qry
WHERE qry.timing = '1'
Another thing I should probably point out, I can't use any stored procedures, I'm quite limited in terms of what I can do as it turns out, which is just great! I also tried to contact the hosting company that is being used, and they came back with nothing useful, I think their English is very broken. They weren't answering a simple question.... So... Any ideas? ¯_(ツ)_/¯
Make sure that timezone support is enabled by mysql:
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
The system is your system timezone, the below query gives time difference between your system and GMT in seconds.
SELECT TIMESTAMPDIFF(
SECOND, TIMESTAMP('2018-02-01 00:00:00'),
CONVERT_TZ(TIMESTAMP('2018-02-01 00:00:00'), 'SYSTEM', 'GMT')
)

how i can get correct time using mysql?

For my website am working with phpmyadmin as a back end. i have number of queries written on mysql in my programs.
Queries are working fine.
> But the problem is i am getting previous date upto 12.30PM.
It is a problem with mysql server. It is delay with 12 hours 32 minutes according to my time.
To display today's date before 12:30 Pm, i have to change all code How i can solve this without changing.
its simple,just use now() method,its is a native mysql method, it will gives you correct time stamp.
guess you want to insert a time into table then use like
INSERT INTO X(time) VALUES (NOW());

mysql now() function

What's the SQL way of doing this:
$now = date('Y-m-d H:i:s', time())
SELECT * FROM table WHERE '$now' > time
Is it:
SELECT * FROM table WHERE now() > time
Your second example is the correct pure SQL method of doing it.
SELECT * FROM table WHERE NOW() > `time`
Although I find it more readable to reverse them, as it seems to make better logical sense to think the value of time is before now`. This really makes no difference though, and is based on my preference.
SELECT * FROM table WHERE `time` < NOW()
There are many more native MySQL date functions you can use in your queries, described in the MySQL documentation.
For example, to compare against 5 minutes ago, use DATE_SUB()
SELECT * FROM table WHERE `time` < DATE_SUB(NOW(), INTERVAL 5 MINUTE)
I don't understand question - first example is calculating date according to your php backend, second using mysql server's builtin function. essentialy effect is the same.
And if you ever considered, now() function in mysql is deterministic (it will replicate correctly to slaves), so even in master-slave environment both snippets of code have no difference, as long as php backend and sql server have synchronized clocks.
just to clarify: of course if your php backend and mysql clocks are not synchronized there will be differences between both snippets of code, but in normal environment this should not happen, at least be irrelevant.