Hi, would something like this be useful?
I'm not sure about the check in the validation part.


/**
 * @file
 * Convert string to timestamp.
 */

$plugin = array(
  'form' => 'feeds_tamper_timetodate_form',
  'callback' => 'feeds_tamper_timetodate_callback',
  'validate' => 'feeds_tamper_timetodate_validate',
  'name' => 'PHP Date Format',
  'multi' => 'loop',
  'category' => 'Text',
);

function feeds_tamper_timetodate_form($importer, $element_key, $settings) {
  $form = array();
  $form['date_format'] = array(
    '#type' => 'textfield',
    '#title' => t('PHP Date Format'),
    '#default_value' => isset($settings['date_format']) ? $settings['date_format'] : '',
    '#description' => t('Format ( eg. d/m/Y )'),
  );
  return $form;
}

function feeds_tamper_timetodate_callback($result, $item_key, $element_key, &$field, $settings) {
  $field = date($settings['date_format'], $field);
}

function feeds_tamper_timetodate_validate(&$settings) {
  if (!date($settings['date_format'])) { // Not sure about this...
    form_set_error('settings][date_format', t('Format must be in correct php date format.'));
  } else {
    $settings['date_format'] = (string) $settings['date_format'];
  }
}

Comments

twistor’s picture

Status: Active » Needs work

Looks good!

A few things:
The name should just be "Date format".
Add a link to http://php.net/manual/en/function.date.php somewhere on the plugin form.
Validation won't do anything, just checking that it's not empty would be enough.
Somewhere on the form it should also mention that it's converting a UNIX timestamp to the date format.

gionnibgud’s picture

StatusFileSize
new1.24 KB

Here you go.
The name now is "Unix timestamp to Date"
Added a better help text with a link to the manual.

Validation now works for empty input and valid date string by doing

  if (strtotime(date($settings['date_format'])) == FALSE) {
    form_set_error('settings][date_format', t('Invalid date date format string.'));
  }

cheers

gionnibgud’s picture

Status: Needs work » Needs review
twistor’s picture

Status: Needs review » Needs work

Feel free to tell me I'm wrong, but I think date() can output many things that strtotime() can't parse. For example, date() could spit out "It's 10 o'clock Jim." That's why it only ever returns an error when the timestamp is incorrect. There's essentially no input that date() won't accept.

gionnibgud’s picture

StatusFileSize
new1.23 KB

Ha! Guess you are right about the date function. You could enter something like this:
date('l jS \of F Y h:i:s A');
Ok

function feeds_tamper_timetodate_validate(&$settings) {
  if (!empty($settings['date_format'])) {
    form_set_error('settings][date_format', t('Please enter a valid date format string.'));
  } else {
    $settings['date_format'] = (string) $settings['date_format'];
  }
}
gionnibgud’s picture

Status: Needs work » Needs review
twistor’s picture

Status: Needs review » Fixed
twistor’s picture

Status: Fixed » Patch (to be ported)
twistor’s picture

Status: Patch (to be ported) » Closed (fixed)
irgnet’s picture

This works perfect when input data is unix timespamp.
But i aggregate several feeds, some feeds using unixtimestamp, other using Date format.
And when i import these feeds with date format, i got errors.

So can you make a validation , which to work only with unix timestamp and skip when input is date format.