Logic Hook - Date Calculation

I have IssuedDate and ExpiryDate as Date fields in A0S_Quotes. They are stored in the format 2020-02-25. I’m trying to create a beforeSave logic hook that calculates the ExpiryDate as 30 days after the IssuedDate.

This is the kind of thing i want to do -

$bean->ExpiryDate = dateSub($bean->IssuedDate, '30');						
function dateSub($dateStr, $days){
	global $timedate;		
	$date = $timedate->fromUser($dateStr);		
	$date->add(new DateInterval('P'.$days.'D'));
	return $timedate->asUser($date);	

I have looked at that many date related posts i’m completely baffled now. I have created a workflow, which works but is extremely slow, but need to set the date within the logic hook as part of other code

I managed to set the date using createFromFormat :

$IssuedDate = DateTime::createFromFormat('Y-m-d', $bean->IssuedDate);			
$IssuedDate->add(new DateInterval('P30D'));		
$bean->ExpiryDate= $IssuedDate->format("Y-m-d");
1 Like

@Bungle Does your solution works when users use different date format? If it does, cool!! If not. Work with dates on DB via query. There, any date is stored on a standard format.



Yes it works for different user date formats - just luck for a change