Skip to Content Skip to Menu

[SOLVED] Calculating Age from Date of Birth Field

  • highonryan
  • highonryan
  • OFFLINE
  • Posts: 60
  • Thanks: 14
  • Karma: 2
7 years 5 months ago - 7 years 5 months ago #294420 by highonryan
Hello,

I am trying to show age by year on a tab based on a date from registration.

I have a field in registration called "cb_dob", which is a date picker. I have a second field called "cb_age" that display's on a custom tab "Registry"

I simply want to show the calculated age in years on the "Registry" based on "cb_dob" from the registration.

(The Registry Tab is a collection of personal questions that are not in the registration process. Also Registry is an optional element, which the users opts to be in after the registration process. I need the DOB on the registration, and would rather not place a second DATE picker on Registry.)

I set up a query field called cb_age that displays on Registry. I added this query:
Code:
SELECT `cb_dob` , CURDATE( ) , TIMESTAMPDIFF( YEAR, `cb_dob` , CURDATE( ) ) AS cb_age FROM `#_comprofiler` WHERE `user_id` = '[user_id]'

When I do a test in MySQL I get the correct age as a number (i.e. 55), but when I place it in Community Builder, it display's the user's birthday as a DATE (i.e. 1961-09-20), but not the age in years.

What am I doing wrong? or Is there a better way of accomplishing this?

Thanks,
HighOnRyan
Last edit: 7 years 5 months ago by krileon. Reason: Added [SOLVED] tag to subject

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48541
  • Thanks: 8290
  • Karma: 1445
7 years 5 months ago #294423 by krileon
Replied by krileon on topic Calculating Age from Date of Birth Field
Edit your date field and under Parameters > Display set "Profile View Display" to "Age". You don't need a query field to have age display as it's built into date fields. Next you can use a Custom HTML field and substitute in your cb_dob so you can have it display on a second tab. See the below for substitution usage information.

www.joomlapolis.com/documentation/279-community-builder/tutorials/18353-using-substitutions-throughout-cb


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.
The following user(s) said Thank You: RebeccaWool

Please Log in or Create an account to join the conversation.

  • highonryan
  • highonryan
  • OFFLINE
  • Posts: 60
  • Thanks: 14
  • Karma: 2
7 years 5 months ago #294438 by highonryan
Replied by highonryan on topic Calculating Age from Date of Birth Field
Again, thanks for the fast reply Krileon.

I tried that before I tried the query, but it only shows the date format.

ARGH, it was a simple mistake, because I pulled it in as [cb_dob], but if I use [cb:userfield field="cb_dob" /] it works flawlessly.

Sorry for the simple post.

Thanks again!
The following user(s) said Thank You: krileon

Please Log in or Create an account to join the conversation.

Moderators: beatnantkrileon
Powered by Kunena Forum