我最近有一个用于简单压光机拾取的PHP编码。但这是mm / dd / yy的日期格式。无论如何,我可以将其自动转换为yy-mm-dd吗?我听说使用strtotime可以在数据库进入数据库时自动对其进行更改,但是我不知道如何以及在何处添加代码。我尝试strftime("%Y-%m-%d", strtotime($Date))
在主页上添加.. ,但是我不知道在哪里添加,但是所有位置均无效。有什么帮助吗?非常感谢您的帮助。
这大致是我主页上的样子:
if(isset($_POST["insert_click"]))
{
$Date=$_POST["Date"];
strftime("%Y-%m-%d", strtotime($Date)); //I tried putting it here but it does not work
$Time=$_POST["Time"];
$query=$connect->prepare("insert into Booking(Date, Time) values (?,?)");
$query->bind_param('ss', $Date, $Time);
$query->execute();
}
这大致是我的表单页面中的样子:
<tr>
<td>Date:</td>
<td>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Datepicker - Format date</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function()
{
$( "#datepicker" ).datepicker({minDate:0});
$( "#format" ).change(function()
{
$( "#datepicker" ).datepicker();
});
});
</script>
</head>
<body>
<input type="text" id="datepicker" size="30" name="Date"/>
</body>
</html>
</td>
</tr>
您的行strftime("%Y-%m-%d", strtotime($Date));
是正确的。您只是忘记了将此函数的返回值分配给变量。您将需要这样的东西:
$newDate = strftime("%Y-%m-%d", strtotime($Date));
如果您不再需要原始日期,则还可以执行以下操作:
$Date = strftime("%Y-%m-%d", strtotime($Date));
注意:这些天来,对于数据库或HTML<time datetime="bla"></time>
标记中使用的PHP时间格式,您可能会很懒。“标准”和本地化时间格式都为您提供了一种生成常用时间值组合的捷径,例如:
c
对于date()%F
对于strftime()本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句