8

So using the $wpdb->update to add some data to a custom table, trying to add the current timestamp but it's not saving the right stuff(0000-00-00 00:00:00 this is saved).

overview code

  $wpdb->update('mytable',
      array(
          'value' => 'hello world', 
          'edit'  => date("Y-m-d h:i:s") //saves 0000-00-00 00:00:00
      ),
      array(
          'option_name' => 'the row'
      ), 
      array('%s, %s')
  );
  • Try: date( "Y-m-d h:i:s", strtotime( time() ); – Sormano Mar 9 '14 at 13:45
  • @Sormano time() returns an integer, not a string. – fuxia Mar 9 '14 at 13:56
  • 1
    You're right, used strtotime() to many times... right code: date( "Y-m-d h:i:s", time() ); – Sormano Mar 9 '14 at 13:58
  • still saving 0000-00-00 00:00:00.... db col(edit timestamp NOT NULL DEFAULT '0000-00-00 00:00:00') – user759235 Mar 9 '14 at 14:06
  • found the issue %d should be %s. but I see that it saves the server time not the current timezone – user759235 Mar 9 '14 at 14:16
11

You seemed to have solved everything but the issue with the time:

found the issue %d should be %s. but I see that it saves the server time not the current timezone

WordPress has a number of date/time related functions. In this case, it sounds like what you need is current_time(), which...

Returns the blog's current local time in one of two formats, either MySQL's timestamp data type format (i.e. YYYY-MM-DD HH:MM:SS) or the Unix timestamp format (i.e. epoch).

So what you should need is:

$wpdb->update(
      'mytable',
      array(
          'value' => 'hello world', 
          'edit'  => current_time( 'mysql' )
      ),
      array(
          'option_name' => 'the row'
      ), 
      array('%s, %s')
);

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.