[英]unix_timestamp date and time difference
I have stored three essential timestamps and times into my database table, format as follows: 我已经在数据库表中存储了三个基本的时间戳和时间,格式如下:
mysql> select receivedtime, requesttime, sla from table;
+---------------------+---------------------+----------+
| receivedtime | requesttime | sla |
+---------------------+---------------------+----------+
| 2013-05-26 22:37:04 | 2013-05-26 12:37:04 | 02:59:59 |
| 2013-05-26 14:36:44 | 2013-05-21 12:39:09 | 72:00:00 |
+---------------------+---------------------+----------+
2 rows in set (0.00 sec)
I need to put a few conditions like below: 我需要提出以下一些条件:
difference = (receivedtime - requesttime);
if [difference <= sla]
{
show meet
} else {
show don't meet
}
legend: 传说:
receivedtime [timestamp] as because days
requesttime [timestamp] as because days
sla [time] as because hour
I have checked UNIX_TIMESTAMP()
of mysql, strtotime()
of php
and few other tries. 我检查了
mysql, strtotime()
UNIX_TIMESTAMP()
, php
mysql, strtotime()
和其他一些尝试。
I also have checked several threads @ stackoverflow.com, but I can't find proper solution. 我也检查了几个线程@ stackoverflow.com,但是找不到合适的解决方案。
Any ideas...! 有任何想法吗...!
Check TIMESTAMPDIFF()
and SEC_TO_TIME()
functions: 检查
TIMESTAMPDIFF()
和SEC_TO_TIME()
函数:
SELECT
`receivedtime`,
`requesttime`,
`sla`,
(SEC_TO_TIME(TIMESTAMPDIFF(SECOND, `requesttime`, `receivedtime`)) < `sla`) as `meet`
FROM
`table`;
Try selecting from your database like this ... 尝试像这样从数据库中进行选择...
select TIMESTAMPDIFF(SECOND, receivedtime, requesttime) as difference, TIME_TO_SEC(sla) as slasecs from table
and then you can do your. 然后您就可以做。
if [difference <= sla]
{
show meet
}
else
{
show don't meet
}
That is a datetime field, not a timestamp field. 那是日期时间字段,而不是时间戳字段。 You should start with reading up on what a timestamp is.
您应该先阅读时间戳记。
you can figure out the difference by converting to timestamp then subtracting. 您可以通过转换为时间戳然后减去来找出差异。
$recieved = strtotime($res['receivedtime']);
$requested = strtotime($res['requesttime']);
$difference = $recieved - $requested;
if($difference <= $sla){
//do stuff
}
You can do that in SQL 您可以在SQL中做到这一点
SELECT (UNIX_TIMESTAMP(receivedtime) - UNIX_TIMESTAMP(requesttime)) AS difference FROM table
it gives you the difference in seconds. 它以秒为单位提供差异。
Try this 尝试这个
select TIMESTAMPDIFF(SECOND,`requesttime`,`receivedtime`) as timeDiff FROM `table`
WHERE TIMESTAMPDIFF(SECOND,`requesttime`,`receivedtime`) > TIME_TO_SEC(`sla`)
You can find relevant in mysql documentation 您可以在mysql文档中找到相关内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.