1

When I try having a hidden value of the date to be inserted into my submission form in an HTML form, it's not working. I have tried multiple different ways to try to get it to work but it just ends up coming back in the database at "0000-00-00".

Here is my HTML code of the hidden value:

<input type="hidden" name="time_date" value="<?php echo date('H:i:s M d, Y'); ?>" readonly="readonly">

In MySQL/PHPmyAdmin, its type is "DATE". I have tried many different ways for the date to work, but no different formats or anything is working. Anyone who is able to help would be greatly appreciated.

3
  • Why don't you do <?php echo time(); ?> for a timestamp and store it as such in the database? Commented Feb 4, 2015 at 0:34
  • That not a proper date format you need to do date('Y-m-d H:i:s') for insertion into the db.. if you want to display it in a different format then you need to convert it before insert and then convert it before display. Commented Feb 4, 2015 at 0:36
  • I have tried that like this: <input type="hidden" name="time_date" value="<?php echo date('Y-m-d H:i:s'); ?> " readonly="readonly" /> and it doesn't work. Still shows up in PHPMyAdmin as all zeros. Commented Feb 4, 2015 at 1:06

4 Answers 4

3

The DATE format in MySQL accepts a date in this format :

YYYY-MM-DD

" The supported range is '1000-01-01' to '9999-12-31'." See doc

Depending on what you want to do, you can use a VARCHAR column if it has to be in this format, or use the DATETIME column type which accepts a date in this format :

 YYYY-MM-DD HH:MM:SS

You can also use a timestamp, I find them easier to handle.

Sign up to request clarification or add additional context in comments.

2 Comments

How would I format that then for this line? <input type="hidden" name="time_date" value="<?php echo date('H:i:s M d, Y'); ?>" readonly="readonly"> I have changed it to DATETIME type. But, I'm not sure how to change this format here ('H:i:s M d, Y').
I got it to work! I used this line: date("Y-m-d H:i:s"). It seems to work with the quotation marks for some reason instead of the single quotation mark inside the "H:i:s M d, Y".
2

Try this instead, the date function uses the output format as the arguement:

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

Edit: I think the problem with your earlier attempts is that you have quotation marks inside of other quotation marks (value's pair and date's pair).

7 Comments

Sorry, I misread what format you wanted. I would verify the field is properly populated by viewing the page source of the form. Also you can try this: <input type="hidden" name="time_date" value="<?php echo date('Y-m-d'); ?>" readonly="readonly">
Is this for the "Date" type? If so, I would like it to be for the "DATETIME" type so that it displays the date and the time. I have tried an answer above yours, but it still didn't work.
If you want to the DATETIME type it sounds like you will need to update your table structure to the new format. Both solutions are showing valid date formats that my MySQL table is accepting, can you post the code you are using to insert it into the database?
This didn't work either. Could this be the problem? It says up at the top when I'm viewing the entry's into the database under "Browse" tab: Showing rows 0 - 84 (85 total, Query took 0.0011 seconds.) [time_date: 0000-00-00 - 0000-00-00]. Then it says right below that: SELECT * FROM cad ORDER BY time_date DESC. Do you want me to upload all of my files somewhere so you can take a look at the code?
I got it to work! I used this line: date("Y-m-d H:i:s"). It seems to work with the quotation marks for some reason instead of the single quotation mark.
|
0

If your datatype is still 'date', just use this. (I tried saving this on a 'date' column, it worked for me)

<input type="hidden" name="time_date" value="<?php echo date('Y-m-d'); ?>" readonly="readonly">

1 Comment

This didn't work either. Could this be the problem? It says up at the top when I'm viewing the entry's into the database under "Browse" tab: Showing rows 0 - 84 (85 total, Query took 0.0011 seconds.) [time_date: 0000-00-00 - 0000-00-00]. Then it says right below that: SELECT * FROM cad ORDER BY time_date DESC
0

This is how i have it in my form, Works a treat and this is how it shows in MYSQL database 30-07-2018 12:16:27.

Also if your form is saved as .html i'm unsure it will work you need your form to be saved as a .php it works for me. if you have already fixed your issue let me know.

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

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.