MySQL timediff function does not give proper output - mysql

This function
SELECT HOUR(TIMEDIFF('2020-06-17 12:15:00am','2020-06-17 01:15:00am')) as 'diff'
gives me the difference as
11 hours
while actually it should be
1 hour
. How do I fix this? Please advice.
Thank you.

MySQL doesn't recognize am and pm by default, it parses times in 24-hour format. You need to use STR_TO_DATE() if you want to parse a custom datetime format.
Also, you need to put the later time first.
SELECT HOUR(TIMEDIFF(STR_TO_DATE('2020-06-17 01:15:00am', '%Y-%m-%d %r'),
STR_TO_DATE('2020-06-17 12:15:00am', '%Y-%m-%d %r'))) as 'diff'

Related

How to convert epoch time to standerd format with timezone in mysql

I saved time in epoch format in database.
Currently I am using this query. It is working without any error but I am not sure about time zone.
SELECT DATE_FORMAT(FROM_UNIXTIME(ctr.creation_time/1000), '%d-%m-%Y %h:%i:%s')
as creation_time
FROM course_transaction_record as ctr
Here ctr.creation_time is 1519193716585
Please let me know how I can set time zone in this query.
We need to add piece of "set" before executing the query
like this
SET time_zone='+05:30';
SELECT DATE_FORMAT(FROM_UNIXTIME(ctr.creation_time/1000), '%d-%m-%Y %h:%i:%s')
as creation_time
FROM course_transaction_record as ctr ;
//for india
You can use CONVERT_TZ(dt,from_tz,to_tz) see here.

How to format time to '2:34 hrs', '0:34 hrs' etc in MySQL

I am writing a query that is selecting two times and calculating the difference between these, where one is the start and the other is the finish to find out the duration. However both are set to TIME types in mysql. How can i format the time instead of showing HH:MM:SS to '[OPTIONAL H]H':MM hrs'
Do i use the extract function?
I presume i have to wrap the TIMEDIFF around something else?
my sql code is as follows:
SELECT operator_no, log_in_time, TIMEDIFF(log_in_time,log_out_time) AS Duration
many thanks
Yes, wrap it in TIME_FORMAT():
SELECT operator_no, log_in_time,
TIME_FORMAT(
TIMEDIFF(log_in_time, log_out_time),
'%H:%i hrs'
) AS Duration
would this not work too?
SELECT operator_no, log_in_time,
CONCAT(EXTRACT(HOUR_MINUTE FROM TIMEDIFF (log_in_time,end_time)) + 'hrs' )
AS Duration

Convert MySQL date and time to datetime

I have a table which has 4 columns. Date login, date logout, time login, time logout.
How can I get the difference. I tried something like this;
SUM(TIMESTAMP(date_logout, time_logout) - TIMESTAMP(date_log, time_log))
However I'm getting a really really high number and I am unable to convert it to proper time. Any hints or clues? I didn't design the table btw :P
Thanks in advance!
if you want the diff in secounds:
SUM(UNIX_TIMESTAMP(TIMESTAMP(date_logout, time_logout)) - UNIX_TIMESTAMP(TIMESTAMP(date_log, time_log)))
If you want the diff in days:
SUM(DATEDIFF(TIMESTAMP(date_logout, time_logout), TIMESTAMP(date_log, time_log)))
If you want the diff as HH:mm:ss
SUM(TIMEDIFF(TIMESTAMP(date_logout, time_logout), TIMESTAMP(date_log, time_log)))
Subtract the two value by using TIMEDIFF FUNCTION.
TIMEDIFF(TIMESTAMP(date_logout, time_logout) ,TIMESTAMP(date_log, time_log) )
TIMEDIFF(TIMESTAMP(date_logout, time_logout) , TIMESTAMP(date_log, time_log))

mysql query not returning results

I am executing this query
SELECT *
FROM temp
WHERE DATE_FORMAT(startTime,'%m/%d/%Y') = '7/15/2012'
and startTime column has this value '2012-07-15 12:00:00'
But this is not returning any results. Can somebody please help?
Change here:
7/15/2012
to:
07/15/2012
According to the documentation for the DATE_FORMAT function, %m is "Month, numeric (00..12)". Note the zero-padding. So you need to write '07/15/2012' rather than '7/15/2012'.
(And in case you're wondering — I have no idea what month #0 is. So far as I'm aware, the months range from 01 to 12. Maybe some locales do have a month #0?)

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