Simple date subtraction on logic_hook; shouldn't this be easy?!

Hello everyone.

I am trying to get the number of days since a date that has been entered into SuiteCRM (whilst retrieving the record on the after_retrieve hook). Surely this should be easy but I’m unable to arrive at the correct number of days.

Any help massively appreciated. Please note I am not a developer and I have no IDE or debug capabilities other than writing values out to the detail view of SuiteCRM!

The code I have so far is:


function calc_rolling($bean, $event, $arguments) {
		$ageindays = 0;
		$rightnow = strtotime("Today");
		$deliverydate = $bean->htcu_fld_articlesreceived_c;
		$newDate = date("Y-m-d", $deliverydate);
		$interval = $rightnow - strtotime($newDate);
		$ageindays = floor(($interval/60/60/40));
		$bean->htcu_fld_debug_c = $ageindays;
	}

But I’m happy to scrap any of that to get this day calculation working! :smiley:

“htcu_fld_articlesreceived” is a date field in Cases. I simply want the number of days since that date.

Regards,

Ste.

Hi Ste,

The TimeDate class in SuiteCRM provides some handy methods for dealing with dates. I’d usually go with something like (untested):

function calc_rolling($bean, $event, $arguments) {
                global $timedate;
		$ageindays = 0;
                $rightnow = $timedate->getNow();
                $deliverydate = $timedate->fromDb($bean->htcu_fld_articlesreceived_c);//Check me, may actually be fromUser
		$interval = $rightnow->diff($deliverydate);
		$ageindays = $interval->days;
		$bean->htcu_fld_debug_c = $ageindays;
	}

Hope this helps,
Jim

Thanks for the very quick reply, Jim.

Unfortunately neither

$deliverydate = $timedate->fromDb($bean->htcu_fld_articlesreceived_c);

nor

$deliverydate = $timedate->fromUser($bean->htcu_fld_articlesreceived_c);

returns a value.

However, if I write out the value of “htcu_fld_articlesreceived_c”, I do get the 23/06/2015 from the database.

Kind regards,

Ste.

Hi Ste,

Can you try both fromUserDate and fromDbDate?

Thanks,
Jim

1 Like

Thanks, Jim. “fromUserDate” did the trick and saves me from a late night in the office!

Thanks again, mate.

Ste.