setFormatter for mariaDB/mysql timestamp?

setFormatter for mariaDB/mysql timestamp?

DigoDigo Posts: 8Questions: 3Answers: 0

Hello!

I tried to select/update a mysql-timestamp in the editor:

Field::inst( 'mydate' )              
    ->getFormatter( Format::dateSqlToFormat('d.m.Y H:i') )
    ->setFormatter( Format::dateFormatToSql('Y-m-d H:i:0' ) ),

but this seems not be the correct way to do this (record ends up with current date, not the date entered)
Any suggestions what I have to use as setFormatter?

Thanks
Digo

Answers

  • rf1234rf1234 Posts: 3,000Questions: 87Answers: 421

    You seem to be retrieving the date in a different format than what you send to the server. That looks a bit odd. Are you really transforming the date format on the client side?

    This is what "dateFormatToSql" does:

    The sql date format in PHP is this: ('Y-m-d H:i:s')
    You seem to be "imitating" that format in your setFormatter. But you need to specify the format that is arriving on the server, not the target database format.

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    Hi Digo,

    In addition to rf1234's comment, one other useful bit of information for us would be to see how you have the field configured on the client-side please.

    Allan

  • DigoDigo Posts: 8Questions: 3Answers: 0

    Hello!

    Thanks for your replies!

    Here is what I want to do:

    • get timestamp from db (format in db: 2022-06-21 14:50:00)
    • display and edit in format: d.m.Y H:i (e.g. 23.09.2022 19:30)
    • save it to db

    ok - now I know, that dateFormatToSql defines the format, I send it to the server.

    So I changed it to:

    Field::inst( 'StartDatum' )
         ->getFormatter( Format::dateSqlToFormat('d.m.Y H:i') )
         ->setFormatter( Format::dateFormatToSql('d.m.Y H:i' ) ),
    

    Now the date is being saved - but time is converted to 00:00

    Config of field at client:
    { data: "StartDatum", className: 'editable' },

    Any suggestions?
    Thanks
    Digo

  • rf1234rf1234 Posts: 3,000Questions: 87Answers: 421

    Just use a custom setFormatter.Then you can append the date coming from the client and render it yourself for database update.

    There is more in this thread and in the docs of course:
    https://datatables.net/forums/discussion/comment/151630/#Comment_151630

  • DigoDigo Posts: 8Questions: 3Answers: 0

    Thanks!

    I added some client configuration

    type:   "datetime",
    format:  "DD-HH-YYYY h:mm",
    

    and now it is working fine. ok. the date-picker is broken, but this seems to be a css problem :-)

    Digo

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    Wow yes - I've never seen it quite so broken. There must be conflicting CSS somewhere.

    If you find something that you think we should add an override for in the date picker's CSS, let me know.

    Allan

Sign In or Register to comment.