Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between two strings with time and weekday

Tags:

php

mysql

I have two times saved in database as

DayTime1 = "Wed 09:00"
DayTime2 = "Wed 13:00"

I want to get the difference between these two dates in minutes.

TIMESTAMPDIFF(MINUTES,DayTime1,DayTime2) return null

I'm not able to get through it. Is there any way to get difference?

like image 872
Parikshit.S.Shekhawat Avatar asked Sep 26 '22 20:09

Parikshit.S.Shekhawat


1 Answers

Please note, that SELECT STR_TO_DATE("Wed 09:00", "%a %H:%i") returns 0000-00-00 09:00:00 (at least on my local MySql 5.5.16). So if comparing different days, you won't get the correct result.

If given a year and week, the day name will be interpreted to a real date, so comparisons may also span days. For example (though not really elegant, I admit):

SELECT TIMESTAMPDIFF(MINUTE,
    STR_TO_DATE(CONCAT(YEAR(CURDATE()), WEEKOFYEAR(CURDATE()), ' Tue 09:00'), '%x%v %a %H:%i'),
    STR_TO_DATE(CONCAT(YEAR(CURDATE()), WEEKOFYEAR(CURDATE()), ' Wed 13:00'), '%x%v %a %H:%i')
)
like image 176
flederwiesel Avatar answered Oct 03 '22 02:10

flederwiesel