Jump to content

Inserting date and time to MySQL DB


zibrnp

Recommended Posts

Hello,

 

- I have date and time in format "12/26/2017 00:00:00".

$raw_date = $row[3]." 00:00:00";

Since I need it in the format "2017-12-26 00:00:00" I am doing

$date_obj = DateTime::createFromFormat("m/d/Y H:i:s", $raw_date);
$formated_date = $date_obj->format('Y-m-d H:i:s');

before I send it to DB I do:

$date = mysqli_real_escape_string($dbc, trim(stripcslashes($formated_date)));

and I end up with "2017-10-06 00:00:00" in the database instead of "2017-12-26 00:00:00".

 

I do not understand why, any suggestion please?

 

 

Link to comment
Share on other sites

I admit I am puzzled.

 

I created a test table

CREATE TABLE datetest (
           date DATETIME NOT NULL PRIMARY KEY
           )
then ran this

$raw_date = '12/26/2017 00:00:00';
$date_obj = DateTime::createFromFormat("m/d/Y H:i:s", $raw_date);
$formated_date = $date_obj->format('Y-m-d H:i:s');

$date = mysqli_real_escape_string($dbh, trim(stripcslashes($formated_date)));

$dbh->query("INSERT INTO datetest VALUES ('$date')");
"2017-12-26 00:00:00" was written to the table, as expected
Link to comment
Share on other sites

    $q = "INSERT INTO pipedrive (id, activity_deal, activity_done, activity_note, activity_subject, activity_due_date, deal_183_RT_lease_number, deal_merchant_number, activity_creator, activity_ID, activity_last_notification_time, activity_marked_as_done_time, activity_type, activity_update_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    $stmt = mysqli_prepare($dbc, $q);
    mysqli_stmt_bind_param($stmt, 'issssssssissss', $id, $activity_deal, $activity_done, $activity_note, $activity_subject, $activity_due_date, $deal_183_RT_lease_number, $deal_merchant_number, $activity_creator, $activity_ID, $activity_last_notification_time, $activity_marked_as_done_time, $activity_type, $activity_update_time);
    mysqli_stmt_execute($stmt);

Do you see anything wrong with this?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.