1

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:

output

Database:

database

I dont know why, and how to fix it.

5
  • Read the documentation... time() doesn't take any arguments and always returns the current time. You're looking for something like strtotime($input) or preferably DateTime::createFromFormat($format, $input)->getTimestamp(). Commented Nov 23, 2013 at 10:13
  • thats by default date store in database....What;s your exact question?? Commented Nov 23, 2013 at 10:14
  • Also, think about what will happen if someone posts a ../index.php in the $_POST['bestand'] field. Commented Nov 23, 2013 at 10:15
  • Mention the type for expire in ur db??? It should be in DATETIME Commented Nov 23, 2013 at 10:21
  • Run a debugger and see what the value of $info['expire'] is. My guess is that it's null. Commented Nov 23, 2013 at 10:21

3 Answers 3

1

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'])); ?>
Sign up to request clarification or add additional context in comments.

Comments

0

try this

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.