当前位置: 动力学知识库 > 问答 > 编程问答 >

PHP date insert

问题描述:

I have a script what insert a date from the value of a input:

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')); ?>" />

You can also edit the date by a date picker!

The datepicker

But when I insert it to the database with this query:

$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'])."')"

And then echo it with this:

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

Output is always the date of to day:

Database:

I dont know why, and how to fix it.

网友答案:

try this

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

Your insert query inserts in database timestamp(converts date to timestamp). For that, Either you insert date as

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

Then,

$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'])."');"

or

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

You have to use strtotime function for this, because time function will always return current time.

Example usage -

In your case echo the date like this -

<?php echo date('d-m-Y H:i:s', strtotime($info['expire'])); ?>
分享给朋友:
您可能感兴趣的文章:
随机阅读: