如何在php中将时间戳转换为正常时间

时间:2019-06-25 15:36:00

标签: php html mysql

我正在尝试将mysql表中的时间戳转换为正常时间。

我尝试在Internet上查找信息,但是只有手动输入的时间戳记解决方案

<?php
//setting header to
header('Content-Type: application/json');

//database
define('DB_HOST', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'bitcoin');

//get connection
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);

if(!$mysqli){
  die("Connection failed: " . $mysqli->error);
}

//query to get data from the table
$query = sprintf("SELECT Kaina, Laiko_Kodas FROM localbtc LIMIT 200");

//execute query
$result = $mysqli->query($query);

//loop through the returned data
$data = array();
foreach ($result as $row) {
  $data[] = $row /*("SELECT date_format(Laiko_Kodas) FROM localbtc")*/;
}

//free memory associated with result
$result->close();

//close connection
$mysqli->close();

//now print the data
print json_encode($data);

2 个答案:

答案 0 :(得分:0)

  while($row = $result->fetch_assoc()) {   
  $data = array(); 
  $data2[]= $row['Kaina'];
  $datetime=$row['Laiko_Kodas'];
  $data2[] = date('M j Y g:i A', strtotime($datetime));
  $dtata[]=$data;
  }

答案 1 :(得分:0)

尝试以下代码:

<?php
//setting header to
header('Content-Type: application/json');

//database
define('DB_HOST', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'bitcoin');

//get connection
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);

if(!$mysqli){
    die("Connection failed: " . $mysqli->error);
}

//query to get data from the table
$query = "SELECT Kaina, Laiko_Kodas FROM localbtc LIMIT 200";

//execute query
$result = $mysqli->query($query);

//loop through the returned data
$data = array();
foreach ($result as $row) {
    $row['Laiko_Kodas'] = date('H:i:s', strtotime($row['Laiko_Kodas']));
    $data[] = $row;
}

//free memory associated with result
$result->close();

//close connection
$mysqli->close();

//now print the data
print json_encode($data);