So, I'm calling data from my database "_gameserver" and putting it into a php as follows. I understand it could be compiled easier and made shorter, but I'm still learning, and not there yet.
<?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();
?>
The "_LastPLayer" string that it grabs from the database is a unix timestamp, I want to convert it into something readable, and then output it with the rest of the data.
I've found what looks like a usable piece of code:
$unix_timestamp = $_POST['timestamp'];
$datetime = new DateTime("@$unix_timestamp");
And I've searched around, but I just cannot figure out how to fit it all together, please help.
Presuming that value is in $row["_TimeCreated"]
You can use create_from_format()
<td>".date_create_from_format('U', $row["_TimeCreated"])->format('F j, Y, g:i a')."</td>
or
<td>".DateTime::createFromFormat('U', $row["_TimeCreated"])->format('F j, Y, g:i a')."</td>
alternatively , you can use date_create() and format() .
<td>".date_create('@'.$row["_TimeCreated"])->format('F j, Y, g:i a')."</td>
or
<td>".(new DateTime('@'.$row["_TimeCreated"]))->format('F j, Y, g:i a')."</td>
Result will be the same: https://3v4l.org/u6EuR .
Finished product, works like a charm! Outputs the unix formatted date into something readable!
<?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();
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.