[英]Calculate difference between two dates( String format) in MySQL
My Client stores the visit time of a page in MySQL as varchar. 我的客户端将页面的访问时间在MySQL中存储为varchar。 He is taking the object of date class new date()
from JS which is off the format 他正在从JS获取日期类new date()
的对象,该对象的格式不正确
Fri Nov 25 2016 12:09:34 GMT+0530 (India Standard Time) 2016年11月25日星期五12:09:34 GMT + 0530(印度标准时间)
I want to calculate the time difference of 2 variables as shown above. 我想计算两个变量的时差,如上所示。 It's used for the analytics purpose, time spent in a page like. 它用于分析目的,花费在类似页面上的时间。
I already tried this: 我已经尝试过了:
SELECT TIMESTAMPDIFF(SECOND, 'Fri Nov 25 2016 12:09:34 GMT+0530 (India Standard Time)',
'Fri Nov 25 2016 12:20:34 GMT+0530 (India Standard Time)')
How can I Do it? 我该怎么做?
You shoud really use mysql Date type. 您应该真正使用mysql Date类型。 (Edit: Just saw in the comment why you're using varchar, so let's skip that ;) ) (编辑:刚在评论中看到了为什么要使用varchar,所以让我们跳过;))
Anyway, you can try to cast your strings into dates using STR_TO_DATE() function 无论如何,您可以尝试使用STR_TO_DATE()函数将字符串转换为日期
SELECT TIMESTAMPDIFF(
SECOND,
STR_TO_DATE('Fri Nov 25 2016 12:09:34 GMT+0530 (India Standard Time)', '%a %b %d %Y %T'),
STR_TO_DATE('Fri Nov 25 2016 12:20:34 GMT+0530 (India Standard Time)', '%a %b %d %Y %T')
)
尝试这个 :
SELECT TIMEDIFF('Nov 25 2016 12:09:34','Fri Nov 25 2016 12:20:34') as diff;
$dat1 = "Fri Nov 26 2016 12:09:34 GMT+0530";
$dat2 = "Fri Nov 25 2016 12:20:34 GMT+0530";
$diff = abs(strtotime($dat2) - strtotime($dat1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
printf("%d years, %d months, %d days\n", $years, $months, $days);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.