Convertint millisecond to date - mysql

I have a column in a table that contains time in milliseconds (2147483647), or so I have told. I am trying to convert it into the actual time (human time), but haven't been so lucky. I have tried everything I thought was going to be of help, though none of the things I have found on google, and on here, have been helpful.
SELECT arrivalTime, FROM_UNIXTIME(uploadTime), "; //UNIX_TIMESTAMP(uploadTime) * 1000 // UNIX_TIMESTAMP() * 1000
The arrivalTime is uploaded in a different time format but I already have that working. I also have a different table using a different time format, which is also working but I am including it here on my post just in case it can be used as a reference or someone might find it useful in their code.
Date_Add('1970-01-01 0:0:0', INTERVAL(uploadTime/1000 - (timeZoneOffset*60)) SECOND) AS uploadTime
Any help or suggestion would be GREATLY appreciated!!!
PS: the current query gives me this 2038-01-18 22:14:07 as the time, which is obviously wrong. And I have also tried this
FROM_UNIXTIME(uploadTime/1000);
but didn't also do what I wanted
PSS: Okay, after asking around, I found out that this 2147483647 is from Android getTimeInMillis from calendar.API. Hope that helps anyone?

I would do something like this in SQL Server
DateAdd(Second, (2147483647/1000), CAST('1970-01-01 00:00:00' AS datetime)) AT TIME ZONE 'Central European Standard Time' AS uploadTime
This gives me the output:
After your update that the milliseconds comes from Android getTimeInMillis():
Using this milliseconds in my function above:
DateAdd(Second, (1566302250040/1000), CAST('1970-01-01 00:00:00' AS datetime)) AT TIME ZONE 'Central European Standard Time'
results into 2019-08-20 11:57:30.000 +02:00 which seems to be right.

Related

DATE_ADD Returning NULL on production server, working in development

Development is localhost running version 5.6.16, production is 5.1.73-cll
The DATE_ADD of this query returns NULL on production, but in development is does exactly what I want it to(adds 90 minutes to the game_time column), The game_time column is a string that contains time in the following format: '21:00'.
This is the query:
SELECT TIME(game_time),
DATE_ADD(TIME(game_time),
INTERVAL 90 MINUTE),
TIME(NOW())
FROM games
What is going on? What am i doing wrong?
I know time should be in a TIMESTAMP, or TIME, but I'm working on someone elses code, I didn't start this from scratch myself.
I've also just noticed that TIME() returns different things, in development, TIME('21:00') returns 21:00:00.000000, in production I only get 21:00:00
Managed to get around, not pretty, but it works.
SEC_TO_TIME(TIME_TO_SEC(TIME(game_time))+5400)
You better develop with the same version as the production server:
Your old version will convert your TIME value to a date and because it's an invalid date, it will get NULL, see manual chapter Conversion Between Date and Time Types
Here's the relevant part:
Before 5.6.4, MySQL converts a time value to a date or date-and-time
value by parsing the string value of the time as a date or
date-and-time. This is unlikely to be useful. For example, '23:12:31'
interpreted as a date becomes '2023-12-31'. Time values not valid as
dates become '0000-00-00' or NULL.
Edit:
To get a TIME value with the desired result, you could use ADDTIME.
This could be working:
SELECT TIME(game_time),
ADDTIME (TIME(CONCAT(CURDATE(), ' ', game_time))),
'01:30:00'),
TIME(NOW())
FROM games
untested, because I have no such old MySQL version anymore.
Try moving the conversion to time outside the DATE_ADD:-
SELECT TIME(game_time), TIME(DATE_ADD(game_time, INTERVAL 90 MINUTE)), TIME(NOW())
FROM games
DATE_ADD works on a DATE or DATETIME field, and as it is you are passing it a TIME field.

Converting to unix timestamp and back again cause loss of 1 day in MySQL

I experienced a problem importing dates into MySQL. I boiled it down to this ...
select from_unixtime(unix_timestamp(str_to_date('201201', '%Y%m')))
It reports...
2011-12-31 00:00:00
To make it return the original date, is there something I need to set up with MYSQL, or do I just fiddle it and add on one day or something?
I'm in the GMT time zone.
A search returned some very old bugs about this and other posts says it was how it is supposed to happen, but I didnt understand what you are supposed to do about it
On 5.5.21 (OS X) i get 2012-01-01 00:00:00.
Try upgrading your server.
When I run it, SELECT STR_TO_DATE('201201', '%Y%m') returns the invalid date 2012-01-00 (January 0th?!), so I'm not altogether surprised that round-tripping that through UNIX_TIMESTAMP() and FROM_UNIXTIME() ends up messing it up. Try adding a day to make it a real date (2012-01-01) first.

MySQL Convert 48 Hours into 48:00:00

I have a regular DATETIME row, eg: 2012-06-19 13:56:56
I am running the following to see how much the time difference is:
DATEDIFF(end_time, NOW()) * 24
It returns 48
Edit: How do I get the Minutes/Seconds? I have tried UNIXTIME(field) - UNIXTIME(NOW()) but i cant get beyond it.
Im trying to convert this into 48:00:00 (Or however that timestamp works)
I keep looking up time functions but they have to do with EXTRACT, and Im not sure thats the way to go about it.
Since datediff only ever returns a difference in days, you might as well just use a string operation to do your formating:
SELECT CONCAT(DATEDIFF(end_time, NOW()) * 24), ':00:00')

Difference in minutes from two time fields in MySQL

I set up my MySQL database with the field 'time'
It is not HH:MM in the traditional sense, it is the time an event occurred, so an event with the value of 5:45 occurred with 5 minutes 45 seconds left in a game. 12:25 occurred with 12 minutes and 25 seconds left, etc.
I would like to be able to find out the total time elapsed, so if I have an event that occurred at 12:25 and the next event occurred at 5:45 I want to be able to get the difference, which would be equal to 6:40. Then, I would like to express this as a decimal, in this case 6.67.
Is this possible?
For me this worked:
TIMESTAMPDIFF(MINUTE, T0.created, T0.modified)
Reference: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_timestampdiff
Just use
TIMEDIFF(fromtime, totime):
SELECT TIMEDIFF("12:25", "5:45");
If you need a decimal, use TIME_TO_SEC()/3600 (it assumes you passed it seconds, but the format you're storing the times in is vague and SQL probably will interpret them as HH:MM - you can fix this with CONCAT("00:",MinuteSecondField) maybe?) - then you can use TIME_TO_SEC()/60, which is more correct)
I needed similar. Two useful functions: TIME_TO_SEC and SUBTIME.
e.g. if your time fields were normal HH:MM times, then
SELECT (TIME_TO_SEC(end_time) - TIME_TO_SEC(start_time))/60 AS `minutes`
In your case, as I understand it, the times are backwards, ie. 12:00<6:00 so your end_time and start_time would need swapping.
If you wanted the output in HH:MM:SS you could do
SELECT SUBTIME(end_time, start_time)
I used UNIX_TIMESTAMP(event1)-UNIX_TIMESTAMP(event2), which gives you seconds between events. Divide it by 60.0 and you will get a decimal as per your requirement. This solution assumes, your columns are date-compatible values. It will work really fast if they are TIMESTAMPs.
UPDATE: This solution only works with timestamp data types, not time datatypes as in original question.

How can SELECT UTC_TIMESTAMP() return -10:00 UTC?

Either I'm being stupid or something's wrong here.
I have two SQL Servers, the one is on my local machine (local time +2 GMT) and the other is somewhere else (NOW() seems to return +8 GMT)and I access it through phpMyAdmin. I have a table that has a DATETIME column. I'm trying
to store the current GMT/UTC time and then display it again, still as GMT/UTC time.
Originally I stored DATE_SUB(NOW(), INTERVAL 8 HOUR) which worked just fine. However, then I read about UTC_TIMESTAMP() and liked it more, as it was shorter and the MySQL manual even said :
"The current time zone setting does not affect values displayed by functions
such as UTC_TIMESTAMP() or values in DATE, TIME, or DATETIME columns."
So perfect right? Except no.
Let's assume Current GMT is 2010-02-18 17:18:17 (I even double checked it with someone in Britain).
On my local (+2) server, I get the following results for the following queries:
SELECT NOW(); 2010-02-18 19:18:17
SELECT UTC_TIMESTAMP(); 2010-02-18 17:18:17
On my online server I get:
SELECT NOW(); 2010-02-19 01:18:17
SELECT UTC_TIMESTAMP(); 2010-02-19 07:18:17 (WHY?!)
Am I missing something?!
Probably because the clock are wrong on the online server?
Try running this:
SELECT ##system_time_zone, NOW(), UTC_TIMESTAMP()
and see which zone does it return and does it match the difference.