【问题标题】:DataTable formatting datetime column in tableDataTable 格式化表中的日期时间列
【发布时间】:2016-06-13 13:38:06
【问题描述】:

我正在使用 DataTable 插件以动态方式显示 mySQL 数据库表。该表对“checkinDateTime”列使用日期时间格式。在我的表格中,它将日期/时间格式化为2016-04-15 14:27:36。我想让它更具可读性,可能格式类似于April 15, 2016, 2:27 pm

这是我的代码:

Reports.php(这是用户可以查看表格的地方。我只会展示 DataTables 脚本及其设置):

<script>
    $(document).ready(function() {

        $('#checkin').DataTable({
        "bProcessing": true,
        "serverSide": false,
        "dom": 'lBfrtip',
     "buttons": [
        {
            extend: 'collection',
            text: 'Export',
            buttons: [
                'copy',
                'excel',
                'csv',
                'pdf',
                'print'
            ]
        }
    ],
        "ajax":{
            url :"response.php", // json datasource
            type: "post",  // type of method  ,GET/POST/DELETE
            data: {}
            }
        });   
});
</script>

Response.php(DataTables 如何拉入数据):

<?php
//include connection file 
include_once("../connection.php");

// initilize all variable
$params = $columns = $totalRecords = $data = array();

$params = $_REQUEST;

//define index of column
$columns = array( 
0 => 'id',
1 => 'suid', 
2 => 'staffMember',
3 => 'studentName',
4 => 'studentEmail',
5 => 'checkinDateTime'
);

$where = $sqlTot = $sqlRec = "";

// check search value exist
if( !empty($params['search']['value']) ) {   
$where .=" WHERE ";
$where .=" ( studentName LIKE '".$params['search']['value']."%' ";    
$where .=" OR staffMember LIKE '".$params['search']['value']."%' ";
$where .=" OR studentEmail LIKE '".$params['search']['value']."%' ";
$where .=" OR suid LIKE '".$params['search']['value']."%' ";
$where .=" OR checkinDate LIKE '".$params['search']['value']."%' )";
}

// getting total number records without any search
$sql = "SELECT * FROM `checkin` ";
$sqlTot .= $sql;
$sqlRec .= $sql;
//concatenate search sql if value exist
if(isset($where) && $where != '') {

$sqlTot .= $where;
$sqlRec .= $where;
}


//$sqlRec .=  " ORDER BY ". $columns[$params['order'][0]['column']]."   ".$params['order'][0]['dir']."  LIMIT ".$params['start']." ,".$params['length']." ";

$queryTot = mysqli_query($VisitorManagement, $sqlTot) or die("database error:". mysqli_error($VisitorManagement));


$totalRecords = mysqli_num_rows($queryTot);

$queryRecords = mysqli_query($VisitorManagement, $sqlRec) or die("error to fetch check-in data");

//iterate on results row and create new index array of data
while( $row = mysqli_fetch_row($queryRecords) ) { 
$data[] = $row;
}   

$json_data = array(
    "draw"            => intval( $params['draw'] ),   
    "recordsTotal"    => intval( $totalRecords ),  
    "recordsFiltered" => intval($totalRecords),
    "data"            => $data   // total data array
    );

echo json_encode($json_data);  // send data as json format
?>

Connection.php(访问数据库):

<?php
$hostname_VisitorManagement = "localhost";
$database_VisitorManagement = "visitor-management";
$username_VisitorManagement = "****";
$password_VisitorManagement = "****";
$VisitorManagement = mysqli_connect($hostname_VisitorManagement, $username_VisitorManagement, $password_VisitorManagement, $database_VisitorManagement);

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

date_default_timezone_set('America/New_York');
?>

我浏览了他们的支持论坛和这里,但我没有找到可以与我的代码一起使用的解决方案。

【问题讨论】:

  • 对我来说不清楚,问题是什么。也许这将有助于展示您最接近的试验。我记得通过 java 脚本、通过 php、通过 SQL 进行日期本地化,有很多层,人们可能想要这样做。但最终知道“数据表”之类的其他人已经知道一个最佳解决方案......

标签: php jquery mysql datetime datatable


【解决方案1】:

我建议通过 MySql 执行此操作,而不是

SELECT * FROM `checkin`

使用

Select field1,field2,..., date_format(checkinDateTime,'%d %b, %Y, %h:%i %p') as checkinDateTime

如果不起作用,请通过datatables's date plugin进行处理

【讨论】:

    猜你喜欢
    • 2019-12-15
    • 2011-11-20
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多