PHP:如何从表单中获取日期/时间,格式化为字符串然后插入数据库?

时间:2018-05-30 07:25:19

标签: php date datetime formatting

所以我有一个表格,里面有大量不同的内容进入数据库,当我将日期和时间检查器添加到表单中时,它们作为字符串进入数据库 (例如:" 2018-05-29"" 10:47:00.000000"他们进入"Here"时的外观如何)

我想重新格式化这些内容,以便当它们插入到数据库中时,它们会显示为实际的单词

(例如:" 2018年5月29日"" 5月29日星期二"" 10:47 AM / PM"这些内容)

因此,当我使用我的php并从数据库中获取数据并将其显示在页面上时,它将显示实际的单词而不仅仅是数字。

(注意:是的,我已经尝试过另一种方式,当它从数据库出来时转换它,但由于我显示的方式,它不会工作)

<?php

$servername = "";
$username = "";
$password = "";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Select Database
mysqli_select_db($conn, "czt_database");

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

echo "Connected successfully";

$game = mysqli_real_escape_string($conn, $_POST['Game']);
$map = mysqli_real_escape_string($conn, $_POST['Map']);
$challenge = mysqli_real_escape_string($conn, $_POST['Challenge']);
$extra = mysqli_real_escape_string($conn, $_POST['Extras']);
$players = mysqli_real_escape_string($conn, $_POST['Players']);
$entryfee = mysqli_real_escape_string($conn, $_POST['Type']);
$entrycost = mysqli_real_escape_string($conn, $_POST['Fee']);
$prizemoney = mysqli_real_escape_string($conn, $_POST['Prize']);
$entrymax = mysqli_real_escape_string($conn, $_POST['Entries']);
$startdate = mysqli_real_escape_string($conn, $_POST['Start-date']);
$starttime = mysqli_real_escape_string($conn, $_POST['Start-time']);
$enddate = mysqli_real_escape_string($conn, $_POST['End-date']);
$endtime = mysqli_real_escape_string($conn, $_POST['End-time']);
$rules = mysqli_real_escape_string($conn, $_POST['Rules']);

$sql = "INSERT INTO tournaments (tournament_game, tournament_map, tournament_challenge, tournament_extra, tournament_players, tournament_entryfee, tournament_entrycost, tournament_prize, tournament_entrymax, tournament_startdate, tournament_starttime, tournament_enddate, tournament_endtime, tournament_rules) 
        VALUES ('$game', '$map', '$challenge', '$extra', '$players', '$entryfee', '$entrycost', '$prizemoney', '$entrymax', '$startdate', '$starttime', '$enddate', '$endtime', '$rules');";
mysqli_query($conn, $sql);

header("Location:../Create-A-Tournament.html");

?>

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码

$startDate=date("l dS M Y", strtotime($_POST['start-date'])); //This will give result as Tuesday 7th May 2018.

$startTime=date("h:i A", strtotime($_POST['start-time']));  // This returns time as 04:56 AM/PM

$startdate = mysqli_real_escape_string($conn, $startDate);
$starttime = mysqli_real_escape_string($conn, $startTime);

然后您可以使用上述变量存储在DB中。

希望它有所帮助。