插入 Php 的日期

Inserting date from Php

我的 CMS 中有一个文本字段,我可以在其中添加最后的游戏结果并 post 将其存入数据库。我唯一缺少并需要帮助的是如何为此添加日期。例如,曼联的最后一场比赛是在 6 月 12 日星期二。我想将该日期放入我的 CMS 和数据库中。我应该怎么做?这是我的代码。

<body>
<form action="index.php?insert_last_game" method="post"> 
    <table width="795" align="center" bgcolor="#FFFFFF">
        <tr bgcolor="#FF6600">
            <td colspan="6" align="center"><h1>Insert Last Game:</h1></td>
        </tr>
        <tr>
            <td align="right" bgcolor="#FF6600"><strong>Game:</strong></td>
            <td><input type="text" name="game" size="60"/></td>
        </tr>
        <tr>
            <td align="right" bgcolor="#FF6600"><strong>Game:</strong></td>
            <td><input type="text" name="date" size="60"/></td>
        </tr>
        <tr bgcolor="#FF6600">
            <td colspan="6" align="center"><input type="submit" name="submit" value="Publish Now"/></td>
        </tr>
    </table>
</form>

和我的 php 部分。

<?php 
include("connect.php");
if(isset($_POST['submit'])){
$post_game = $_POST['game'];
$post_date = date('d-m-y');
if($post_game==''){
echo "<script>alert('Please fill in all fields')</script>";
exit();
}
else {
$insert_game = "insert into last_game (game,date) values ('$post_game','$post_date')";
$run_posts = mysqli_query($con,$insert_game); 
echo "<script>alert('Post Has been Published!')</script>";
echo "<script>window.open('index.php?insert_last_game','_self')</script>";
}
}
?> 

以上是添加游戏的日期。我想从文本字段中输入日期,如图所示。 谢谢

首先,在您的 HTML 中,您将第二个标签命名为游戏,而不是日期。

<td align="right" bgcolor="#FF6600"><strong>Game:</strong></td>

为了做你想做的事,你必须在 PHP

$post_game = $_POST['game'];
$time = strtotime($_POST['date']);
$post_date = date('d-m-y',$time);

您已经完成了剩下的工作。

如果您希望将日期格式设置为 Tue, 12th of Junuary,您可以这样做:

$post_date = date('D, jS', $time) . ' of ' . date('F', $time);

也祝贺您使用mysqli_

用strtotime添加,见http://php.net/manual/pt_BR/function.strtotime.php

显示日期,示例:

$test = strtotime("now");
echo date("jS F, Y", $test);