Skip to Content Skip to Menu

The CB Code field plugin (version 1.0.2 or better) supports 5 additional field types that get their select values from a database query.

These new field types are:

  • Code
  • Code Check Box (Multiple)
  • Code Drop Down (Multi-select)
  • Code Drop Down (Single Select)
  • Code Radio Buttons

You can create as many CB Fields as you want of each field type.

The fields work just like their core counterparts but generate output based on provided PHP code.

Our use case is to create a new CB Field (cb_codedaysold) that calculates the exact years, months and days based on an existing birthday field (cb_birthday).

With the latest CB Code field plugin (version 1.0.2 or better) installed and published you will be able to implement the cb_codedaysold field as follows:

  1. Create a new CB Field from CB Field Management area
  2. Select the 'Code' type for your new field
  3. Give the field a name (e.g., codedaysold) and a title (e.g., Days Old)
  4. Populate the Code parameter area of the field with:

    $datetime1 = new DateTime();
    if ($user->cb_birthdate == "0000-00-00") return 'No birthday specified';
    $datetime2 = new DateTime($user->cb_birthdate);
    $difference = $datetime1->diff($datetime2);

    return 'Exact age is: ' . $difference->y . ' years, ' . $difference->m . ' months, ' . $difference->d . ' days';

 

On completion you should be able to see your Days Old out similar to that of following screenshot:

 

daysold

Alternative code

For those of you who are CB API savvy, you can use following 2 liner instead of previous code:

$difference = \CBLib\Application\Application::Date( 'now', 'UTC' )->diff( $user->cb_birthdate );
return 'Exact age is: ' . $difference->y . ' years, ' . $difference->m . ' months, ' . $difference->d . ' days';