Javascript JQUERY根据SQL时间戳字段和NOW()之间的时间差更改div的文本

Javascript JQUERY根据SQL时间戳字段和NOW()之间的时间差更改div的文本,javascript,php,jquery,Javascript,Php,Jquery,我在下面有一个脚本,可以从sql中提取一些数据: <?php require 'admin-db.php'; // Define and perform the SQL query $sql = "SELECT `id`, `first_name`, `last_name`, `status_time` " . "FROM `staffs` ORDER BY `id` DESC"; $result = $DB_CON->query($sql);

我在下面有一个脚本,可以从sql中提取一些数据:

<?php
  require 'admin-db.php';

  // Define and perform the SQL query
  $sql = "SELECT `id`, `first_name`, `last_name`, `status_time` "
       . "FROM `staffs` ORDER BY `id` DESC";
  $result = $DB_CON->query($sql);

  // If the SQL query is succesfully performed ($result not false)
  if($result !== false) {
    $data_row = '<table class="table table-striped tablesorter">'
              . '<thead>'
                . '<tr>'
                . '<th>ID</th>'
                . '<th>Fist Name</th>'
                . '<th>Last Name</th>'
                . '<th>Status Time</th>'
                . '</tr>'
              . '<tbody>';

    foreach($result as $row) {
      $data_row .=  '<tr>'
                  . '<td scope="row" class="text-center">'.$row['id'].'</td>'
   . '<td>' .$row['first_name'].'</td>'
   . '<td>' .$row['last_name'].'</td>'
   . '<td><div class="status">' .$row['status_time']. '</div><span class="fa fa-times-circle"></span></td>'; 
    }
  }
  $data_row .=  '</tbody>' 
              . '</table>';
  echo $data_row;

这可能就是你要找的

// foreach tr row
$.each($('tr .status'), function(i, v) {
  // get seconds from date
  var seconds = (Date.now() - Date.parse($(v).text()))/1000;
  if(seconds) {
    (seconds < 30) ? $(this).text('1') : $(this).text('0');
  }
});
//foreach tr行
$。每个($('tr.status'),函数(i,v){
//从日期开始计算秒数
var seconds=(Date.now()-Date.parse($(v.text())/1000;
如果(秒){
(秒<30)?$(this.text('1'):$(this.text('0');
}
});

如果您告诉我时间返回哪种格式状态,我可以将这一次实现到我的scriptCool中!谢谢你,麦琪。如果我的php脚本中foreach循环下的$row['status\u time']以秒为单位返回,则代码可以工作。但是,$row['status_time']以这种格式返回时间:“2017-05-07 18:36:22”。我想,为了使代码正常工作,我们需要将php脚本更改为$row['status_time'],以返回此上下文中“2017-05-07 18:36:22”与现在时间()之间的时差(以秒为单位)。我试过了NOW()-.$row['status_time'].';但它返回的是2017年。@MasterJoe在php time diff中,您需要从数据中创建Date对象,然后可以使用Date->diff(time())或使用strotime函数转换日期并从中减去。我尝试了使用Date_diff的建议,但不知怎的,我遇到了各种各样的错误。我认为错误在于两个时间段不相同的字符串格式。我在搞砸这件事时犯了各种各样的错误。如果我声明$now=date(“Y-m-dh:I:s”),我如何确定秒数的差异?我尝试了date\u diff($now,$row['status\u time']),但没有成功。我的sql字段“status\u time”是datetime格式的。所以我尝试(strotime($now)-strotime($row['status\u time']),其中$now=date(“Y-m-dh:I:s”),得到了14401的返回。如何将14401转换为秒?看起来14400是以秒为单位的,但巨大的差异是由时区错误造成的。时间戳以EST记录时间,日期时间以UTC记录时间。我不知道它为什么那样做。这个php日期时间垃圾是如此多的bug,并且有一种令人讨厌的方法来做事情,这使得新手更加困惑。这就像在一家不知名的餐厅用餐,菜单上有无数的项目,上面有一大堆希腊语的符号。
// foreach tr row
$.each($('tr .status'), function(i, v) {
  // get seconds from date
  var seconds = (Date.now() - Date.parse($(v).text()))/1000;
  if(seconds) {
    (seconds < 30) ? $(this).text('1') : $(this).text('0');
  }
});