Use Database NOW() in setting value on preCreate

Use Database NOW() in setting value on preCreate

davykiashdavykiash Posts: 35Questions: 13Answers: 1
edited June 2017 in Free community support

Hello,

I would like to set a timestamp before creating a record using the database time stamp value.

Therefore insted of setting time like this

->on( 'preCreate', function ( $editor, $values ) {
        $editor
         ->field( 'datetime_created' )->setValue( '2014-03-19 11:44:40' );
        } )

I intend to set it like this

->on( 'preCreate', function ( $editor, $values ) {
        $editor
          ->field( 'datetime_created' )->setValue( 'NOW()' );
        } )

Any help?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 64,076Questions: 1Answers: 10,566 Site admin
    Answer ✓
    ->on( 'preCreate', function ( $editor, $values ) {
            $editor
              ->field( 'datetime_created' )->setValue( date('c') );
            } )
    

    Should do the business I think.

    Another option is to use the DEFAULT option in your SQL column and have it DEFAULT now() rather than needing PHP to do it. Personally that's the way I would do it :).

    Allan

This discussion has been closed.