CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Proper format for custom field date values using API?
Pages: [1]

Author Topic: Proper format for custom field date values using API?  (Read 1866 times)

greenmachine

  • I post occasionally
  • **
  • Posts: 58
  • Karma: 6
Proper format for custom field date values using API?
July 25, 2008, 07:40:41 pm
I'm running 2.0.5 and trying to create a bunch of contacts using the API's civicrm_contact_add function. Everything OK except for a custom field set up as a Date field that I'm trying to add data to.

The problem is I can't get the date value formatted properly. Each one I've tried results in the custom field value showing up in CiviCRM as 0th, 0 and in the database table as 0000-00-00 00:00:00.

Code: [Select]
$params = array(
  "contact_type"  => "Individual",
  name, address, etc.
  "custom_10" => $regdate
  );
$contact =&civicrm_contact_add($params);

Here are formats I've tried:

YYYYMMDD
YYYY/MM/DD
YYYY-MM-DD
YYYYMMDD
YYYY-MM-DD 00:00:00
unix timestamp (integer)

Some of those (YYYYMMDD) result in a "is not of type Timestamp" non-recoverable error, but the others don't report any errors. They just don't get recorded right.

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Proper format for custom field date values using API?
July 25, 2008, 09:08:41 pm
The proper format is YYYYMMDD or YYYYMMDDhhmmss

there is a bug in 2.0 which is filed and fixed here: http://issues.civicrm.org/jira/browse/CRM-3354

The patch can be found here:

http://fisheye.civicrm.org/changelog/CiviCRM/?cs=16160

lobo
« Last Edit: July 27, 2008, 11:35:47 pm by Donald Lobo »
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

bartclarkson

  • I’m new here
  • *
  • Posts: 4
  • Karma: 0
  • CiviCRM version: 4.2
  • CMS version: Drupal 7
  • MySQL version: 5.x
  • PHP version: 5.x
Re: Proper format for custom field date values using API?
September 11, 2012, 05:21:54 am
Quick example to spell out the code associated with YYYYMMDDhhmmss, which works in 4.x. 

Here's a snippet I took from a hook_civicrm_post() and generalized for example, wherein I needed to save a date to a custom date field.

*Moderator: Would have used the code tokens, but getting bug "Sorry, you are not allowed to post external links."

Quote
// If you're moving a date from one place to another, strtotime() for timestamp;
// $ts = strtotime($values['contribution.recent_date']);
$ts = time();
$date_to_save = date("YmdHis", $ts);
$custom_field_id = 5;
// Get this from whatever objects passed to the hook;
// $contact_id = $objectRef->contact_id;
$contact_id = 1234;

$customParams = array("entityID" => $contact_id,
  "custom_$custom_field_id" => $date_to_save,
);

require_once 'CRM/Core/BAO/CustomValueTable.php';
CRM_Core_BAO_CustomValueTable::setValues( $customParams );

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Proper format for custom field date values using API?

This forum was archived on 2017-11-26.