php/Mysql 查询插入日期失败
我已经搜索了很长一段时间,但我不明白为什么我的查询不适用于日期。
我希望它作为日期导入到我的数据库中,这是上级的选择,所以我在这里。
$insert_query = "INSERT INTO enrties(
`datum` )
VALUES (
'".mysql_real_escape_string($_SESSION['Datum'])."')
在上面的几页中,我使用这段代码来填充 $_SESSION
$DateDag = $_POST['Dag'];
$DateMaand = $_POST['Maand'];
$DateJaar = $_POST['Jaar'];
switch ($dateMaand)
{
case 'Januari': $DateMaand = '01'; break;
case 'Februari': $DateMaand = '02'; break;
case 'Maart': $DateMaand = '03'; break;
case 'April': $DateMaand = '04'; break;
case 'Mei': $DateMaand = '05'; break;
case 'Juni': $DateMaand = '06'; break;
case 'July': $DateMaand = '07'; break;
case 'Augustus': $DateMaand = '08'; break;
case 'September': $DateMaand = '09'; break;
case 'Oktober': $DateMaand = '10'; break;
case 'November': $DateMaand = '11'; break;
case 'December': $DateMaand = '12'; break;
}
$_SESSION['Datum'] = $DateDag. '-'. $DateMaand. '-'. $DateJaar;
我希望将日期导入为 SQL 日期格式(最好是 DD-MM-YYYY 格式)。
已解决,开关中有一个错误显然,不太确定是什么,但它有效;)
I have been searching for quite a while now and I can't figure out why my query isn't working for dates.
I want it to be imported to my database as a Date, this is a choice from higher up so here I am.
$insert_query = "INSERT INTO enrties(
`datum` )
VALUES (
'".mysql_real_escape_string($_SESSION['Datum'])."')
a few pages before the above i use this piece of code to fill the $_SESSION
$DateDag = $_POST['Dag'];
$DateMaand = $_POST['Maand'];
$DateJaar = $_POST['Jaar'];
switch ($dateMaand)
{
case 'Januari': $DateMaand = '01'; break;
case 'Februari': $DateMaand = '02'; break;
case 'Maart': $DateMaand = '03'; break;
case 'April': $DateMaand = '04'; break;
case 'Mei': $DateMaand = '05'; break;
case 'Juni': $DateMaand = '06'; break;
case 'July': $DateMaand = '07'; break;
case 'Augustus': $DateMaand = '08'; break;
case 'September': $DateMaand = '09'; break;
case 'Oktober': $DateMaand = '10'; break;
case 'November': $DateMaand = '11'; break;
case 'December': $DateMaand = '12'; break;
}
$_SESSION['Datum'] = $DateDag. '-'. $DateMaand. '-'. $DateJaar;
I want to have the date imported as a SQL Date format (preferably DD-MM-YYYY format).
SOLVED, there was a mistake in the switch Apparantly, not really sure what but it works ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MySQL 日期格式必须为 YYYY-MM-DD
将其翻转,如下所示:
MySQL date format has to be YYYY-MM-DD
Turn it around so it looks like:
MySQL 日期格式实际上是 YYYY-MM-DD,因此如果您想插入日期(猜测您的数据字段是日期或日期时间),您必须以这种方式设置字符串。
如果你想以不同的方式插入日期,你应该使用这个:
但这将始终以 YYYY-MM-DD 方式保存日期,因为这是 mysql 的默认方式。
The MySQL date format is actually YYYY-MM-DD, so if you want to insert a date (guessing your datum field is Date or Datetime) you have to set up your string in that way.
If you want to insert your date in a different way you should use this:
but this will save always the date in YYYY-MM-DD way, since is the default for mysql.
插入日期后,您可以在查询后进行格式化
$new_format=date('h:i:sd/m/Y',strtotime($date_string));
after date is inserted you can format after query
$new_format=date('h:i:s d/m/Y',strtotime($date_string));