[RESOLVED] Adding current date into an SQL table as a hidden field

As the title really - I am just trying to enter the current date into a field when a form s submitted.

I tried:

<input name="2016_Registered_Date" type="hidden" value="<?php echo date();?>" >

With no luck, so added some a date format:

<input name="2016_Registered_Date" type="hidden" value="<?php echo date('j F Y');?>" >

It seems to get the day and month, but not the year, eg 8 July 6638

Any suggestions what else to try?


If you just want it to go into the database table, why bother putting it into the form as a hidden variable? Just create a column for it, and have it default to the current date/time if nothing is entered (which it won’t be if you don’t have a form field for it).

1 Like

The issue there is that there are other forms which update other fields, so wouldn’t want this field to update to the current date when they are submitted.

If you set the database column to default to current date, it will not update the date field unless you give it a date to update with. If you don’t provide the date for that column, it won’t UPDATE. But it will enter the current date for an INSERT.



Thanks - this is for an UPDATE.

So how would I give it the date? It must be close, as the code above gets the day and month right, but gets the year wrong.

I know you can use TIMESTAMP to do this, but I am already using that for a LastUpdated field. And I thought mySQL only lets you use one of those in a table?

I’m confused. You’re asking how to give it a date for UPDATE; but in post #3, you indicate that you DO NOT want it to update the date field:

If you do not want the date field to update to anything during an UPDATE, don’t supply the date and don’t refer to it in the column names of the UPDATE tableName portion. If you DO want the date updated, supply the date in the same format as the database natively formats it. (ie, if the database formats dates as “yyyy/mm/dd”, then supply the date as 2015/07/08. If the database formats as dd-mmm-yy, then supply the date as 08-JUL-15; then refer to the column in the UPDATE tableName portion, as well as provide the date in the SET section.)

UPDATE tableName
SET id='{id}', name='{name}', date='{properFormattedDate}'
WHERE something = {something}

UPDATE tableName
SET id='{id}', name='{name}' /* don't supply date */
WHERE something = {something}

Sorry - wires crossed.

I have a table:


And two UPDATE forms, one where all the Lodge info can be updated, and I use a TIMESTAMP field to record the date of that change.

And a second UPDATE form where someone can just tick a box to record that they are registering the lodge to (to appear on the website). And we would like to record the date they do that.

So I was just saying that I didn’t want the 2016_Registered_Date field to default to the current date any time the record is updated, because then it would update when either of those forms is submitted.

Which is why I was looking at using a hidden field in the registration form.

It looks like it works when I changed the date format, so changing:

<input name="2016_Registered_Date" type="hidden" value="<?php echo date('j F Y');?>" >


<input name="2016_Registered_Date" type="hidden" value="<?php echo date('Y-m-d H:i:s');?>" >

Thanks for taking the time to reply - always appreciate it.

You should take in the input of the checkbox on the server and at that point, set the 2016_Registered_Date field, before you store it.

if(isset($_POST['formRegisteredCheckbox']) && 
   $_POST['formRegisteredCheckbox'] == 'Yes') 
    $2016_Registered_Date = time(); 

If you have the date created at the time of the form creation and send it to the user as a hidden field in the form, then the user could relatively easily change the value of the hidden field in the form and thus falsify their registration date. Not sure if it is really important, but the chance is there.


1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.