Insert Date Field in Plugin

By Ryan - August 26, 2019

Hi, i have created the plugin below that creates a record in a log table each time a particular user saves a record in a master table. 

However i can't get the date (masterRefDate) to insert correctly. The date appears to be broken into it's year month and date components, how do i recombine them?

Thanks,

Ryan

addAction('record_postsave', 'plugin_addRevision', null, 4);

   function plugin_addRevision() {

  	global $SETTINGS, $CURRENT_USER, $tableName, $isNewRecord, $oldRecord;

  	if ($CURRENT_USER['username'] == 'admin') {

  		if ($tableName == 'mastertable')	{

  			$tablename = 'logtable';
  			$colsToValues = array(); // 
  			$colsToValues['createdDate='] = 'NOW()';
  			$colsToValues['updatedDate='] = 'NOW()';
  			$colsToValues['createdByUserNum' ] = $CURRENT_USER['num'];
  			$colsToValues['updatedByUserNum' ] = $CURRENT_USER['num'];

  			$colsToValues['ref'] = @$_REQUEST['masterRef'];
  			$colsToValues['refDate=']  = $_REQUEST['masterRefDate'];

  			$hideMissingFieldErrors = true;
  			$newRecordNum = mysql_insert( $tablename, $colsToValues, $hideMissingFieldErrors );
  		}
}
}

By daniel - August 26, 2019

Hi Ryan,

If you're using a date field, you should be able to use the following snippet to retrieve the correct format:

$colsToValues['refDate']  = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $_REQUEST["masterRefDate:year"], $_REQUEST["masterRefDate:mon"], $_REQUEST["masterRefDate:day"], _getHour24ValueFromDateInput("masterRefDate"), (int) @$_REQUEST["masterRefDate:min"], (int) @$_REQUEST["masterRefDate:sec"]);

Let me know if that does the trick!

Thanks,

Daniel
Technical Lead
interactivetools.com

By Ryan - August 27, 2019

Worked perfectly, thanks Daniel.