Editor field formatter from unix time

Editor field formatter from unix time

nidedinidedi Posts: 13Questions: 1Answers: 0
edited June 2013 in General
I'm not sure how to get the time converted from unix (like '1370363054') to a more human readable format.
I've tried this, but no luck so far:
[code]
....
$editor = Editor::inst( $db, 'positions' )
->fields(
Field::inst('pos'),
Field::inst('data')
->setFormatter( 'Format::date_format_to_sql', 'D, d M y' ),
....

[/code]

It still displays in unix timestamp.
Also, does this conversion affect sorting?

Replies

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    What I think you might want is something like this:

    [code]
    Field::inst('data')
    ->getFormatter( function ($val) {
    return date( 'D, d M y', $val );
    } )
    [/code]

    The built in `Format::date_format_to_sql` methods expected the date in ISO8601, so you need to have your own formatter - in this case using date to convert to what you want.

    What I haven't got there is a set formatter. The set formatter will take whatever you submit (what will that be?) and convert it into what is stored in the database. In this case I presume you'll want to convert it to a unit timestamp.

    > Also, does this conversion affect sorting?

    It can yes. DataTables will correctly sort any string as a date if `Date.parse()` can parse the string. if it can't you either need to use orthogonal data (through mData / mRender) or a sorting plug-in.

    Allan
  • nidedinidedi Posts: 13Questions: 1Answers: 0
    Thank you for the clear answer! Worked perfectly and now I also understood the syntax.

    I've mixed up set formatter with the get formatter. For now I won't be using editing for this table, so all I needed was the correct get formatter.
This discussion has been closed.