简体   繁体   English

PHP / MySQL试图让TIMESTAMPDIFF输出结果

[英]PHP/MySQL trying to get TIMESTAMPDIFF to output a result

My intention with the below code is display the difference between two timestamps in seconds, but nothing is showing at all. 我使用以下代码的目的是以秒为单位显示两个时间戳之间的差异,但是什么也没有显示。 I´ve added echo $ts for you to see what $ts contains: 我添加了echo $ ts,让您了解$ ts包含的内容:

$ts = $value['time'];
echo $ts;
$sql = "SELECT TIMESTAMPDIFF(SECOND,(CURRENT_TIMESTAMP),'$ts')";
$diff = mysql_query($sql);
echo $diff;

The result is: 结果是:

2017-01-26 16:30:51

Hence it only shows the content of $ts, not what the time difference is. 因此,它仅显示$ ts的内容,而不显示时差。 Help with understanding what I´m doing wrong would be appreciated, thanks! 有助于理解我做错了的事情,谢谢!

mysql_query() function returns: mysql_query()函数返回:

  1. For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error. 对于SELECT,SHOW,DESCRIBE,EXPLAIN和其他返回结果集的语句,mysql_query()成功时返回资源,错误时返回FALSE。

  2. For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error. 对于其他类型的SQL语句,例如INSERT,UPDATE,DELETE,DROP等,mysql_query()成功返回TRUE,错误返回FALSE。

  3. The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data. 返回的结果资源应传递到mysql_fetch_array()和其他用于处理结果表的函数,以访问返回的数据。

So you can try the following: 因此,您可以尝试以下操作:

$con = mysql_connect("localhost", "root", "mypass") or
    die("Could not connect: " . mysql_error());
mysql_select_db("tutorials");//**your database name**//

$sql = "SELECT TIMESTAMPDIFF(SECOND,(CURRENT_TIMESTAMP),'$ts') as timestamp";
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    printf("diff: %s", $row[0]);  
}

mysql_close($con);

Using Php only. 仅使用PHP。

$datetime1 = strtotime($ts);
$datetime2 = strtotime(date('Y-m-d H:i:s'));
$interval  = abs($datetime2 - $datetime1);
echo 'Diff. in seconds is: '.$interval; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM