Creating a calculated field to count days until a date

Hello, Good evening.
i have a problem, just to the point
its possible in SuiteCRM to create a calculated field to count days until a date?
i have googled and i found this
Creating a calculated field to count days until a date
but in SuiteCRM i didnt find the “Calculated Field” in studio and integer data type.
so how i can create that field.



Calculated fields are not currently included in SugarCRM CE/SuiteCRM.



Thank you for the respond Will,

So, how i can create some kind like that?
can you give me any suggestion?

Thankyou very much.


Hi Agas,

You would need extensive knowledge of SugarCRM/SuiteCRM and PHP/MySQL/AJAX etc.

Calculated fields are on the roadmap, but there is no release date as yet.



Are you a developer or just a SuiteCRM common user? If first is your case then I might help you as I created a calculated field that tells the years between 2 dates.

Just let me know.

Btw, this is my first post here. Thanx for your unvaluable help and I hope to give my two cents :wink:

Feel free to share your code as it may help others as starting point in making similar modifications.

Hi, chemimartinez77
im just a common user, but i want to develop too.
so , would you share the code?

Hi Will,

sure, im still studying and learning all about SuiteCRM and PHP/MySQL too.

so there was no a clue for create this?


Sorry for being a bit late but yesterday was local holiday in Valencia so I didn’t check the forums. Ok, here we go. Remember that this code doesn’t accomplish what you are trying to but counts years between 2 given dates (birthdate and current date). Anyway, it should be easy to adapt it.

First we begin by adding the field definition to the module. In my case, it is Contacts, change Contacts for your module name as your convenience.
Create custom\Extension\modules\Contacts\Ext\Vardefs\sugarfield_XXXXXXXX.php where XXXXXXXX is the name of your calculated field. And below you find the definition:

$dictionary[‘Contact’][‘fields’][‘age_year_calc’] = array( //change age_year_calc for XXXXXXXX. Careful, Contact!!! Not Contacts!!! Module name in singular.
‘name’ => ‘age_year_calc’, //Again change age_year_calc for XXXXXXXX
‘vname’ => ‘LBL_AGE_YEAR_CALC’, //You’ll have to add this label to the language files you’re managing. Not sure if you can do this from studio.
‘type’ => ‘varchar’, //As you can see, I chose varchar. This is because I show “N/A” if birthdate is empty or “Error” when birthdate is greater than current date.
‘len’ => ‘3’, //Nobody is older than 999, right? xD
‘source’ => ‘non-db’, //<- Important as it is calculated and we don’t want to store it in the DB (at least not yet).
//‘studio’ => ‘visible’, //I can’t remember how I added it to the views, but maybe this makes the task easier for you (I was such a noob and still I am xD)

Well, I guess until this point everything was quite clear for any familiarized SuiteCRM user. Now we’ll fight against the Logic Hook but in fact he’s our friend :slight_smile: If you want more info on Logic Hooks there are lots of out there. Sorry for not getting deeper.

We have to edit 2 files.

1.- Edit custom\modules\Contacts\logic_hooks.php and add the following lines of code.

$hook_array[‘after_retrieve’] = Array();
$hook_array[‘after_retrieve’][] = Array(1, ‘Here goes YOUR description’, ‘custom/modules/Contacts/logic_hooks_class.php’,‘logic_hooks_class’, ‘f_age_year_calc’);

For your info, I’ll explain the 2nd line a bit. Array has 5 parameters:
1.1 -> 1 is the index of the hook in the after_retrieve array. By the way, we use after_retrieve because after retrieving the field (hence the name) from DB we wanna show it in the Detailview page. If I remember well, you should add the same function to [‘process_record’] in order to diplay the field in the Listview. Don’t ask me why but it worked that way :confused:
1.2 -> Any text you wanna use for describing your hook.
1.3 -> Route to the file where your logic hooks class is stored. Don’t know if you can change the name.
1.4 -> Name of the class (found inside the file previously named).
1.5 -> Name of the class function we want to use.

2.- Edit or create custom\modules\Contacts\logic_hooks_class.php It should look like below. Some comments also to help you.

	if (!defined('sugarEntry') || !sugarEntry) die('Not a valid Entry Point');
	class logic_hooks_class{ //Name we used in point 1.4

		function f_age_year_calc($bean, $event, $arguments){ //Name we used in point 1.5
			////// $bean->age_year_calc <- This is our important field.
            $focus = $bean->custom_fields->retrieve();
            if ($bean->birthdate != null){
                $currentDate = new DateTime();
				$currentDateformat = $currentDate->format('Y-m-d');
				$dateDB = $bean->birthdate;
				$dateDBimport = DateTime::createFromFormat('d/m/Y', $dateDB);
				$dateDBformat = $dateDBimport->format('Y-m-d');
				$dateDBformatDate = new DateTime($dateDBformat);
				$dateDifference = $currentDate->diff($dateDBformatDate);
				$bean->age_year_calc = $dateDifference->y; //I think you could use $dateDifference->d so you have days between those 2 dates.
				if ($dateDBformat > $currentDateformat){
			} else {
				$bean->age_year_calc = "N/A";

I hope all this helps you. Please, let me know if adding ‘studio’ => ‘visible’ to the vardef file makes the work. If not, I’ll search for the insetion of the field in Detailview and Listview.

Kind regards!!!

1 Like

Hi Chemi, thanks for the code and the explaination and sorry for the late respond.

i will try that code.

Best Regards


Sorry bro, in the following line, dunno why, I forgot some code:

There you go:

$dictionary[‘Contact’][‘fields’][‘age_year_calc’] = array( // That’s why I told about witing ‘Contact’ and not ‘Contacts’.


Wow, it did it again. I’ll use code then:


$dictionary['Contact']['fields']['age_year_calc'] = array(
	'name' => 'age_year_calc',
	'vname' => 'LBL_AGE_YEAR_CALC',
	'type' => 'varchar',
	'len' => '3',
	'source' => 'non-db',


Oh dear, everytime I re-read the post I find a new something missing. Here you have a line that should be this way:

1.- Edit custom\modules\Contacts\logic_hooks.php and add the following lines of code.

$hook_array['after_retrieve'] = Array();
$hook_array['after_retrieve'][] = Array(1, 'Here goes YOUR description', 'custom/modules/Contacts/logic_hooks_class.php','logic_hooks_class', 'f_age_year_calc');

Sorry again!

1 Like

This kept me busy for hours until I figured out that dates in beans are strings formatted as per user preferences. I’m using UK format so the code failed. Sugar provides a global variable $GLOBALS [ ‘timedate’ ] which is a TimeDate with the current user’s format and a convenience function to create a SugarDateTme from the user-formatted date string. SugarDateTime is derived from a DateTime…

if ($bean->birthdate != null){
	// Create a SugarDateTime from user-formatted date
	$birthDate = $GLOBALS['timedate']->fromString($bean->birthdate);
	$today = new SugarDateTime();
	if ($today > $birthDate) {
		$bean->age_year_calc = $today->diff($birthDate)->y;
	else {
		$bean->age_year_calc = "Error";
} else {
	$bean->age_year_calc = "N/A";

I hope this saves some time. For some reason the forum does not display the variable properly in text. The crucial line is:

$birthDate = $GLOBALS['timedate']->fromString($bean->birthdate);

How did you create that calculated field? Any guidance you provide will be appreciated.


I did it as per @chemimartinez77 above. Basically, it’s done in a after_retrieve logic hook so the field is set on the bean before it is displayed.

I was only contributing the fact the dates are formatted according to user prefs.


Thanks, for solution!

But I have promblem, the after_retrieve hook don’t work in the subpenel :frowning:

Is it way to calculate non-db field in the subpenel?

Answer on my question is “process_record”

Creating calculated fields to count days until a date and many more functionalities are now available here:

Get your 30 days trial version and see how you can get the best out of your SuiteCRM!

This seems to be part of workflows since a couple of years ago and can be done using SweeterCalc (which is free btw)