gpt4 book ai didi

php - 如何使用 php 将文本字段的内容转换为日期格式?

转载 作者:行者123 更新时间:2023-11-30 23:05:14 24 4
gpt4 key购买 nike

我一直在尝试将文本字段的内容转换为日期格式,然后再将其保存到我的数据库中,但我尝试的一切似乎都失败了。用户输入的日期示例类似于“24/02/2014”(dd/mm/yyyy)

表单代码:

<td>Date of Test:</td>
<td><input type="text" name="Date1" id="dot2"/></td>

来自表单操作页面的代码:

$dateoftest = strtotime($_POST['Date1']);
$newdate = date('Y-m-d', $dateoftest);

$sql="INSERT INTO `tableOne`(`DateofTest`) VALUES ('$newdate')";

代码段是与问题相关的所有代码。数据库字段是“日期”类型。该代码不存储输入的值,而是存储 0000-00-00。

最佳答案

我猜你的日期格式是错误的,根据 php.net strtotime works with:

注意:

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.*

关于php - 如何使用 php 将文本字段的内容转换为日期格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22195759/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com