I am very fairly new to MySQL and wondered the best way to create a age column and also a date field that records the date that the user submitted a form into the database.
There is no sence to create column āageā. Personās age changed itself permanently. You should to save personās birthday. Age is calculatable attribute of query, if you need it.
I see. If I wanted to see their age, I would calculate it when I select the data as per @r937ās comment? If so, then there is not a way to see it in the database when I log into phpmyadmin where I can see the other data such as the date?
Yes, that is probably the best way to retrieve an age from the database.
If you view the table in PHPmyadmin, you would see dates in the table, not the DOB. The age needs to be calculated from the difference of the DOB to the present date. The table shows the actual data you store, your app/page/whatever shows data as you wish it to be represented.
Effectively storing data can be a different way of thinking. Age is a good example of that.
I you want to know someoneās age, you may directly ask their age, not ask their DOB and work it out.
But in data, a DOB is a constant, it does not change.
Whereas age is a relative value and changes all the time.
To store the actual age, you would have to run a script every day to see if itās anyoneās birthday, then update any age it finds. Easier to store a constant value that never changes.
Iām struggling to find a date field that will work in all browsers.
Would it work if I were to use a text field with DD/MM/YYYY as a placeholder? If this is then entered to the database, would I still be able to calculate birthdays?
Browsers that donāt support date inputs, revert to text anyway, so you could still use date.
Though date pickers may display like DD/MM/YYY, what results in post is YYYY-MM-DD, which is good for the database, though maybe not a familiar format for non-dev users. But it may be a better hint format for the fall-back in non-supporting browsers.
Another option is individual day, month, year fields. Again, these are new input types.
Iām sure date pickers has been discussed before, but I donāt recall the outcome.
It may be worth a topic spilt.