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

时间:2017-05-07 22:34:06

标签: 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);

  // 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;

我如何更改我的脚本(见下文)来比较$ row ['status_time']和NOW(),以指示时间不同是否超过30秒它返回0并且如果少于30秒它返回一个1作为div class =“status”中的文本?

<script>
    $('.status:contains(">30 seconds") = $this.text(0) else $this.text(1);
</script>

1 个答案:

答案 0 :(得分:0)

这可能就是你要找的东西。

// 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');
  }
});

JSFIDDLE DEMO