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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.