AppleTattooGuy
AppleTattooGuy

Reputation: 1175

Trying to add WooCommerce booking start date to write into database

I am using WooCommerce Booking plugin and I need to hook into the booking, get the start date and import that into the database as a UNIX timestamp format.

This is the code I am using to try and get the start date:

$wcBooking = new WC_Booking( $original_order_id );
$current_timestamp = strtotime($wcBooking->get_start_date());

I am trying to get it using the WC_Booking class, I have pasted this class here http://pastebin.com/JGjDKyrj

When this code is running, it's just putting the following in the database:

1970-01-01 00:00:00

Thanks in advance for your help.

Upvotes: 1

Views: 1507

Answers (2)

AppleTattooGuy
AppleTattooGuy

Reputation: 1175

Got it the following code works:

$wcBooking = new WC_Booking( $original_order_id );
            $dataString = $wcBooking->get_start_date( 'Y-m-d', 'H:i:s' );
            $current_timestamp = strtotime($dataString);

Upvotes: 1

Patrick Moore
Patrick Moore

Reputation: 13354

You need to format in MySQL date format, if the column is DATETIME type, MYSQL is expecting YYYY-MM-DD hh:mm:ss format, for four digit year, two-digit month and day, and hour and minute and seconds. When it's not receiving this, it replaces with UNIX epoch time as you describe.

One line change, where you reformat in correct date format from UNIX time:

$current_timestamp = date( 'Y-m-d H:i:s', strtotime($wcBooking->get_start_date()) );

You may also set date/time parameters in get_start_date() like:

$current_timestamp = $wcBooking->get_start_date( 'Y-m-d', 'H:i:s' );

Upvotes: 0

Related Questions