PHP日期插入

时间:2013-11-23 10:09:23

标签: php date

我有一个脚本,它从输入值中插入日期:

PHP:

<input type="text" id="datepicker" name="expire" placeholder="dd-mm-yyyy" value="<?php echo (DateTime::createFromFormat('d-m-Y', date("d-m-Y"))->modify('+1 day')->format('d-m-Y')); ?>" /> 

您还可以通过日期选择器编辑日期!

The datepicker

但是当我使用此查询将其插入数据库时​​:

$mysql->query("INSERT INTO code(code, file_match, file_size, expire, ip, can_used) 
VALUES ('".$mysql->real_escape_string($_POST['code'])."',
'".$mysql->real_escape_string($_POST['bestand'])."', 
'".filesize('./Hier_je_files/'.$mysql->real_escape_string($_POST['bestand']))."', 
'".time($mysql->real_escape_string($_POST['expire']))."',
'".$mysql->real_escape_string($_POST['ip_whitelist'])."', 
'".$mysql->real_escape_string($_POST['used'])."')"

然后用这个回声:

<?php echo date('d-m-Y H:i:s', $info['expire']); ?>

输出始终是当天的日期:

output

数据库:

database

我不知道为什么,以及如何解决它。

3 个答案:

答案 0 :(得分:1)

您的插入查询插入数据库时​​间戳(将日期转换为时间戳)。 为了那个原因, 您可以将日期插入

$_POST['expire'] = time($mysql->real_escape_string($_POST['expire']));
$_POST['expire'] = date("d-m-Y H:i:s",$_POST['expire']);

然后,

$mysql->query("INSERT INTO code(code, file_match, file_size, expire, ip, can_used) VALUES ('".$mysql->real_escape_string($_POST['code'])."', '".$mysql->real_escape_string($_POST['bestand'])."', '".filesize('./Hier_je_files/'.$mysql->real_escape_string($_POST['bestand']))."', '".$_POST['expire']."', '".$mysql->real_escape_string($_POST['ip_whitelist'])."', '".$mysql->real_escape_string($_POST['used'])."');"

<?php echo date('d-m-Y H:i:s', strtotime($info['expire'])); ?>

答案 1 :(得分:0)

试试这个

   <?php echo date('d-m-Y H:i:s', strtotime($info['expire'])); ?>

答案 2 :(得分:0)

您必须使用strtotime函数,因为time函数将始终返回当前时间。

使用示例 -

在你的情况下回复这样的日期 -

<?php echo date('d-m-Y H:i:s', strtotime($info['expire'])); ?>