AnsweredAssumed Answered

CE Calculated fields: Days difference between today and creation date

Question asked by arturo.hernandez on Jul 3, 2015
Latest reply on Jul 6, 2015 by arturo.hernandez
Hi everybody,

I am trying to calculate the days difference beetween the create date and todays date, i need the logic to make the calculation. This is what I have: (is not working)

In logic_hooks.php:

<?php

$hook_version = 1;    $hook_array = Array();
    $hook_array['before_save'] = Array();
    $hook_array['before_save'][] = Array(
        //Processing index. For sorting the array.
        1,
       
        //Label. A string value to identify the hook.
        'calculate',
       
        //The PHP file where your class is located.
        'custom/modules/Accounts/logic_hooks_class.php',
       
        //The class the method is in.
        'logic_hooks_class',
       
        //The method to call.
        'before_save_DaysOld'
    );

?>


In logic_hooks_class.php:

<?php

if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');   
    class logic_hooks_class
    {
          function before_save_DaysOld($bean, $event, $arguments)       
          {
                   $date1=date_create($bean->todays_date);
                   $date2=date_create($bean->entered_date);
                   $diff= date_diff($date1,$date2);
                   $stgdiff = $diff->format("%a");
                   $bean->total_days_old_c = $stgdiff;
            }
    }

?>

Outcomes