3

I'm trying to support date formatting of a tag's output, per the Plugin Development & Template Class documentation. I have this code on the backend, with some redundancy on the timestamp value to ensure that, yes, this is a timestamp!

$append = array(
        'timestamp' => strtotime(date('Y-m-d H:i:s',$row['timestamp'])),
        'username' => $row['username'],
        'screen_name' => $row['screen_name'],
        'email' => $row['email']
    );
$output .= $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $append);

If I use the tag {timestamp} in my template, I get the timestamp value: 1363956000.

If I use {timestamp format="%l, %F %j, %Y - %g:%i %A"} in my template, the output is: {timestamp format="%l, %F %j, %Y - %g:%i %A"}.

According to the docs, this should just work, though – Template Class/Date Variables.

How do you support formatting of timestamp values in a plugin's output?

4
  • I've just tried adding a gmmktime() var to one of my fieldtype's response arrays and it does support formatting automatically. The only thing that strikes me is that you seem to be passing a single row to parse_variables rather than an array of them. I'm surprised that's working at all, but maybe that's a factor? Is your sample code copied verbatim?
    – Dom Stubbs
    Mar 24, 2013 at 17:59
  • Second part did the trick. If you want to switch your comment to an answer, I'll give you the credit. Thanks! Mar 24, 2013 at 18:23
  • Nice, glad to hear that worked. I've fleshed out my comment a bit and reposted it as an answer.
    – Dom Stubbs
    Mar 24, 2013 at 18:32
  • And you answered how I get total_results in another tag. Thanks! Mar 24, 2013 at 19:22

2 Answers 2

2

It looks as though you're passing a single row to parse_variables (which normally expects an array of rows). If you switch to parse_variables_row that should help:

You may also parse the result rows yourself, which could be useful if for some reason you need to modify the tagdata for each row based on certain criteria. You can still benefit from the simplified variable parsing by using parse_variables_row(), though you will no longer automatically have {count}, {total_results}, or {switch=} variables. To include these variables when parsing your own result rows, you will need to add them yourself.

If you'd prefer to retain count, total_results etc then you can also easily tweak your existing code by wrapping $append in its own array:

$output .= $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, array($append));
1

Following @Dom Stubbs' recommendation I used gmmktime(), it said it was type:int:

'timestamp' => gmmktime(date('H',$row['timestamp']),
    date('i',$row['timestamp']),
    date('s',$row['timestamp']),
    date('m',$row['timestamp']),
    date('d',$row['timestamp']),
    date('Y',$row['timestamp'])
    ),
        'username' => $row['username'],
        'screen_name' => $row['screen_name'],
        'email' => $row['email']
    );
var_dump($append);

Further following Dom's recommendation, switching to $this->EE->TMPL->parse_variables_row instead of $this->EE->TMPL->parse_variables is returning the correct value.

I was then able to switch back to:

$append = array(
        'timestamp' => $row['timestamp'],
        'username' => $row['username'],
        'screen_name' => $row['screen_name'],
        'email' => $row['email']
    );
$output .= $this->EE->TMPL->parse_variables_row($this->EE->TMPL->tagdata, $append);

Thanks, Dom!

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.