1

I'm working on a migration where I have

2012-05-07 00:00:00.000000

in my timedate field, but I need to convert it to:

2012-05-07

I realize that I should put this in the prepareRow function, but I'm not sure what it should look like.

1
public function prepareRow(Row $row) {

   $date = $row->getSourceProperty('database_date_field');
   $date = strtotime($date); // -> 1336341600 unix timestamp
   $date = date('Y-m-d', $date); // -> "2012-05-07"

   // Here you 'rewrite' the field with the new value before it gets saved
   $row->setSourceProperty('database_date_field', $date);
   return parent::prepareRow($row);
}
1

Why not use a format_date process plugin:

process:
  field_my_date:
    plugin: format_date
    from_format: 'Y-m-d H:i:s'
    to_format: 'Y-m-d'
0

If the source property containing that value is, say, "timestamp", then in prepareRow you can do:

$row->setSourceProperty('timestamp', strtok($row->getSourceProperty('timestamp'), ' '));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.