在输出之前转换mysql输入unix时间戳到php日期时间

5q4ezhmt  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(254)

所以,我调用数据库中的数据“\u gameserver”,并将其放入php中,如下所示。我知道它可以编译得更简单,也可以缩短,但我仍在学习,还没有。

<?php
$servername = "localhost";
$username = "USERNAME";
$password = "PASS";
$dbname = "_gameserver";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT _Key, _Data, _Name, _Ammo, _Cash, _Model, _Flags, _Faction, _SteamID, _SteamName, _Inventory, _LastPlayed, _TimeCreated FROM characters";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
echo "<table style='border: solid 3px black;'>
<tr><th>Key</th>
<th>Steam ID</th>
<th>Steam Name</th>
<th>Last Played</th>
<th>Character Name</th>
<th>Description</th>
<th>Faction</th>
<th>Inventory</th>
<th>Loaded Ammo</th>
<th>Money</th>
<th>Character Model</th>
<th>Character Created</th>
<th>Char Flags</th></tr>";

$unix_timestamp = $_POST['_TimeCreated'];
$datetime = new DateTime("@$unix_timestamp");

// output data of each row
while($row = $result->fetch_assoc()) {
    echo "<tr><td style='width:150px;border:3px solid black;'>".$row["_Key"]."</td>
<td style='width:150px;border:3px solid black;'>".$row["_SteamID"]."</td>
<td style='width:150px;border:3px solid black;'>".$row["_SteamName"]."</td>
<td style='width:150px;border:3px solid black;'>".$row["_LastPlayed"]."</td>
<td style='width:150px;border:3px solid black;'>".$row["_Name"]."</td>
<td style='width:150px;border:3px solid black;'>".$row["_Data"]."</td>
<td style='width:150px;border:3px solid black;'>".$row["_Faction"]."</td>
<td style='width:150px;border:3px solid black;'>".$row["_Inventory"]."</td>
<td style='width:150px;border:3px solid black;'>".$row["_Ammo"]."</td>
<td style='width:150px;border:3px solid black;'>".$row["_Cash"]."</td>
<td style='width:150px;border:3px solid black;'>".$row["_Model"]."</td>
<td style='width:150px;border:3px solid black;'>".date_create_from_format('U', $row["_TimeCreated"])->format('F j, Y, g:i a')"</td>
<td style='width:150px;border:3px solid black;'>".$row["_Flags"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>

它从数据库中获取的“\u lastplayer”字符串是一个unix时间戳,我想将其转换为可读的内容,然后将其与其余数据一起输出。
我找到了一段看起来很有用的代码:

$unix_timestamp = $_POST['timestamp'];
$datetime = new DateTime("@$unix_timestamp");

我到处找过了,但我不知道怎么把它们放在一起,请帮帮我。

a11xaf1n

a11xaf1n1#

成品,像一个魅力工程!将unix的日期输出为可读的内容!

<?php
$servername = "localhost";
$username = "USERNAME";
$password = "PASS";
$dbname = "_gameserver";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT _Key, _Data, _Name, _Ammo, _Cash, _Model, _Flags, _Faction, _SteamID, _SteamName, _Inventory, _LastPlayed, _TimeCreated FROM characters";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
echo "<table style='border: solid 3px black;'>
<tr><th>Key</th>
<th>Steam ID</th>
<th>Steam Name</th>
<th>Last Played</th>
<th>Character Name</th>
<th>Description</th>
<th>Faction</th>
<th>Inventory</th>
<th>Loaded Ammo</th>
<th>Money</th>
<th>Character Model</th>
<th>Character Created</th>
<th>Char Flags</th></tr>";

// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td style='width:150px;border:3px solid black;'>".$row["_Key"]."</td>
<td style='width:150px;border:3px solid black;'>".$row["_SteamID"]."</td>
<td style='width:150px;border:3px solid black;'>".$row["_SteamName"]."</td>
<td style='width:150px;border:3px solid black;'>".$row["_LastPlayed"]."</td>
<td style='width:150px;border:3px solid black;'>".$row["_Name"]."</td>
<td style='width:150px;border:3px solid black;'>".$row["_Data"]."</td>
<td style='width:150px;border:3px solid black;'>".$row["_Faction"]."</td>
<td style='width:150px;border:3px solid black;'>".$row["_Inventory"]."</td>
<td style='width:150px;border:3px solid black;'>".$row["_Ammo"]."</td>
<td style='width:150px;border:3px solid black;'>".$row["_Cash"]."</td>
<td style='width:150px;border:3px solid black;'>".$row["_Model"]."</td>
<td style='width:150px;border:3px solid black;'>".date_create_from_format('U', $row["_TimeCreated"])->format('F j, Y, g:i a')"</td>
<td style='width:150px;border:3px solid black;'>".$row["_Flags"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
vltsax25

vltsax252#

假定该值在 $row["_TimeCreated"] 您可以使用create \u from \u format()

<td>".date_create_from_format('U', $row["_TimeCreated"])->format('F j, Y, g:i a')."</td>

<td>".DateTime::createFromFormat('U', $row["_TimeCreated"])->format('F j, Y, g:i a')."</td>

或者,可以使用date\u create()和format()。

<td>".date_create('@'.$row["_TimeCreated"])->format('F j, Y, g:i a')."</td>

<td>".(new DateTime('@'.$row["_TimeCreated"]))->format('F j, Y, g:i a')."</td>

结果相同:https://3v4l.org/u6eur.

相关问题