how i can get correct time using mysql? - 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());

Related

Mysql date ok but signaled as incorrect

I am trying to insert the date below into my TIMESTAMP column but it's unexpectedly signaled as incorrect format by mysql :
INSERT INTO `assets`( `updated_at`) VALUES ('2022-03-27 02:12:31.217573')
If i add or substract one day or one hour then the date is executed without error.
I going crazy ^^
Thx for your help
mysql version : 5.7.36
timestamp columns are weird. They effectively store a utc time, but on input and output convert to the timezone of the connection. Presumably you are using a timezone that has daylight savings where the second after 2022-03-27 01:59:59 is 2022-03-27 03:00:00, so your entered time is invalid.
If you intend to be storing times that are already in UTC (which you should!) you need to set your timezone appropriately. See https://dev.mysql.com/doc/refman/5.7/en/time-zone-support.html. But be aware that then all previously entered times will appear to change.
I much prefer to use datetime columns which will store and show exactly the data entered, though they have their own quirks.

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 query current time - interval not working

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

Time on server with Rails

So ... I inserted an entry into a table with time stamps. The entry got inserted perfectly.
But the time stamp that got inserted was :
'2015-06-22 10:32:47'
I checked the time on the server:
Tue Jun 23 01:35:41 IST 2015
And inside mysql :
select now();
2015-06-23 01:33:57
So I check the time in irb.
time = Time.now
=> 2015-06-23 01:36:41 +0530
time.zone
=> "IST"
The time zone is right .. but I dont know where is it picking up the time from? I cant figure out where is it picking up 10:32 from ? and how do I fix it ?
Appreciate all your help.
Rails by design converts a datetime into UTC before inserting the value into the database.
This is because, in general, it's not possible to easily represent timezones in the database. Moreover, this approach guarantees the date will be consistent and/or easy to query in the following cases:
if the server time zone changes or you change server with a different timezone
if you want to query the date using a different timezone
if you want to format the date with different time zones depending on the user or other configs
You can customize the default timezone for the app, so that Rails will properly compute the correct difference to UTC when the value is stored into the database. However, you cannot change this behavior: times will always be converted into UTC before being stored in the database.
Generally speaking, if you properly use the Rails date transformation helpers, this shouldn't cause any issue.
For instance, if you want to query all the records where the time is past, the following query
Record.where('some_date < ?', Time.current)
will properly convert Time.current from your app timezone into UTC, and query the database accordingly.

Convert MySQL times tamp column from one timezone to another

Need some ideas on how to convert an entire database TIMESTAMP columns from one timezone to another.
The current server is UTC, MySQL is also UTC so everything is good in that area. All time related columns are TIMESTAMPs. The problem is that when the time information was being entered, they were in EST/EDT. For example, enter start time: data is 1/1/2011 08:00:00 AM (EST/EDT). Because timezone wasn't implemented at the start, the database stored this as 08:00:00 UTC. So all the data in the database is stored like this. Once we get data that requires timezone info, this model will break.
The question is: how do you convert all these TIMESTAMP columns into the correct UTC time? The code will be changed to deal with this on the display side on a go-forward basis but what about historic data?
The simplest way seems to do a mysqldump --tz-utc of some sort and then import the data back, then release the code. However, I can't seem to find a good example of how to do this properly or if there are other ways to do this in the most efficient way possible.
Thanks!
Could you use the MySQL AddTime function to update the existing data?
UPDATE MyTable SET MyTimeColumn = ADDTIME (MyTimeColumn, -8:00:00) WHERE <the data is bad>