Hi,

I am having a odd problem with the date field (Date module).

If I create a node manually, using a string as a date say 2/13/2103 it correctly saves to the node.

However, if I pass the same exact string through an RSS field using Feed Import, it doesn't work.

I get this message:

PDOException: SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2/13/2013' for column 'field_pub_date_value' at row 1: INSERT INTO {field_data_field_pub_date} (entity_type, entity_id, revision_id, bundle, delta, language, field_pub_date_value) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6); Array ( [:db_insert_placeholder_0] => node [:db_insert_placeholder_1] => 953 [:db_insert_placeholder_2] => 449 [:db_insert_placeholder_3] => video [:db_insert_placeholder_4] => 0 [:db_insert_placeholder_5] => und [:db_insert_placeholder_6] => 2/13/2013 ) in field_sql_storage_field_storage_write() (line 448 of /home/xxxxxx/public_html/modules/field/modules/field_sql_storage/field_sql_storage.module)
CommentFileSizeAuthor
#6 datefeedimport.png14.33 KBnetentropy
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Sorin Sarca’s picture

Hi, you have to create a custom filter for that and usit it in Filters section. Example:

function my_module_to_date($date) {
  $date = explode('/', $date);
  return  (object) array(
    'month' => $date[0],
    'day' => $date[1],
    'year' => $date[2],
  );
}

I don't remember right now the date field structure, but it is something like that.
You have to return an object with properties that match field properties.

netentropy’s picture

Cool. Are we supposed to add filters directly to the filters included file or is their a hook where we can write a module for custom filters.

Sorin Sarca’s picture

A filter can be any PHP function, so you can put your functions where you want, just make sure they are loaded.

netentropy’s picture

Any other ideas on this? I find it odd that the node form will accept a typed string yet won't accept it from the xpath. I can add dates pragmatically to nodes but that doesn't work in this case either.

Thanks again!

Sorin Sarca’s picture

Create manually a node, fill a date and use dpm() to print show node properties. Upload here the image containing data relevant to date field.

netentropy’s picture

FileSize
14.33 KB

Thanks again for taking a look. Here is the requested image.

Sorin Sarca’s picture

Here is an example:

function convert_to_date_field($date, $format = 'Y-m-d h:i:s') {
 $date = strtotime($date);
 $data = date($format, $date);
  return (object) array(
    'value' => $date,
    'timezone_db' => 'America/New_York',
    'date_type' => 'datetime',
  );
}
netentropy’s picture

It only worked for me with a capital H

function convert_to_date_field($date) {
$date = strtotime($date);
$date = date("Y-m-d H:i:s", $date);
return (object) array(
'value' => $date,
'timezone_db' => 'America/New_York',
'date_type' => 'datetime',
);
}

Sorin Sarca’s picture

Status: Active » Closed (fixed)
aniebel’s picture

Hi, I am doing this same thing and despite creating this custom module, I'm still getting invalid date format error when trying to import. A few questions:
1.) what's the syntax for adding the new function to the filter? Do I still add the "::" before?
2.) does it matter what the widget type is for the date field? Suppose it's a select list where it becomes an array?

I'd like to write up my full example of a user import via XML once I get it working if that helps others.

aniebel’s picture

Issue summary: View changes
Status: Closed (fixed) » Active
Sorin Sarca’s picture

1) You can use any php or Drupal function, just write its name and desired parameters.
For example, if you want to make all text uppercase you can use php's strtoupper function.
If a class provides a static method (function) you can use it by specifying both class name and method separated by :: (double colon).
Feed Import provides some filter functions encapsulated in FeedImportFilter class (as static methods). This is a special case when you are not required to specify class name (FeedImportFilter will be assumed), only the function name.
You can see all provided filters here https://drupal.org/node/1363028
So if you want to replace some text you can simply use ::replace. It is still correct to use FeedImportFilter::replace.
Even if php has str_replace function it will not work on multivalued fields, but methods from FeedImportFilter takes care of those cases.

2) Should not matter. There are hundreds of field types in Drupal, and I cannot support all of them. But you are able to have a field with all properties by returning an object (simplest way is to cast the array to object), as in above examples of date field.
You can found info about field properties with devel module.

Hope this helps.

aniebel’s picture

Thanks for the kind feedback. My issue was the input date was year only. As soon as I changed it to 2002-12-31 instead of 2002, it worked like a charm. Will be posting my findings once I do a complete import.

Sorin Sarca’s picture

Status: Active » Closed (fixed)
docans’s picture

Hi

I have a text field date value in the format

Original Entry Time:
2000-03-22T11:45:00.000
Status Change Time:
2000-03-22T00:00:00.000

How do i convert it. Which php code is most suitable

Sorin Sarca’s picture

Hi @docans,

please open a new issue and describe your problem (using more details) so I can help you.

Thanks!

docans’s picture

Status: Closed (fixed) » Active