ili
ili

Reputation: 139

submit the current date alongside the rest of the form

I'm trying to get the current date to be submitted to the database without the user entering a date.

I currently have the following php code

<?php $currentDate = time(); ?>

And then within the form ive assigned the php code as a value to a hidden input.

<input type="hidden" name="mDateJoined" value="$_POST['currentDate']">

this doesnt seem to work though? where am i going wrong?

Upvotes: 0

Views: 5787

Answers (4)

Xpleria
Xpleria

Reputation: 5873

You dont have to send the date through the form. In the php code that processes your script, just use the now() function in the query

Like:

INSERT into `users` (`name`,`date`) VALUES('$name',now())

In your database, your date column must be of date type.

OR

date_default_timezone_set('UTC');
$date = date('Y-m-d');

INSERT into `users` (`name`,`date`) VALUES('$name','$date')

Upvotes: 1

Ravi
Ravi

Reputation: 2086

I guess

<?php $currentDate = time(); ?>

and

<input type="hidden" name="mDateJoined" value="$_POST['currentDate']">

are on same page. Try this instead of above code lines:

<?php $currentDate = date('Y-m-d'); ?>

<input type="hidden" name="mDateJoined" value="<?php echo $currentDate; ?>">

I will prefer do not pass the date as hidden field in the form and use <?php $currentDate = date('Y-m-d'); ?> at the place where you are inserting the date into the database.

Upvotes: 0

Roman  Kliuchko
Roman Kliuchko

Reputation: 499

You need to set current date exactly to hidden field:

<input type="hidden" name="mDateJoined" value="<?php echo time();">

And when handling the submitted data you can do:

$currentDate = (new \DateTime())->setTimestamp((int) $_POST['mDateJoined']);

Upvotes: 0

N.DeNisse
N.DeNisse

Reputation: 149

If these both statements are on the same page just change:

<input type="hidden" name="mDateJoined" value="<?php echo $currentDate; ?>">

This will print the value of $currentDate in your hidden field .

Upvotes: 0

Related Questions