2

I can't seem to store the datetime to the database.

public function add_post($username, $content) {
    date_default_timezone_set("Asia/Manila");
    $date = date("m/d/Y h:i A");
    $final = strtotime($date);
    $time_posted = date("m/d/Y h:i A", $final);

    $sql =" INSERT INTO POSTS (username, post_content, time_posted)
            VALUES ('$username','$content','$time_posted')";

        return $this->db->query($sql);
}

It looks like this in the database 0000-00-00 00:00:00

Here is my posts table: Posts table

0

3 Answers 3

3

The date-time field should be formatted as:

$time_posted = date("Y-m-d H:i:s", $final);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Glad it worked. One minor suggestion though - unrelated to datetime / timestamp. Please do take a look at MySQLi and PDO Prepared Statements to write queries in a rather secure way. If this was a live system, then it might be exposed to SQL Injection Attacks over the internet.
Thank you for the suggestion :)
1

Help this code for you,

public function add_post($username, $content) {
   date_default_timezone_set("Asia/Manila");
   $date = date("m/d/Y h:i A");
   $final = strtotime($date);
   $time_posted = date("Y-m-d H:i:s", $final);

   $sql =" INSERT INTO POSTS (username, post_content, time_posted)
        VALUES ('$username','$content','$time_posted')";

    return $this->db->query($sql);
}

Comments

1

MySQL time-stamp has a fixed pattern for date time input i.e. "Y-m-d H:i:s", If you want to insert date time into database then convert the pattern to the above mentioned one and then try. It'll work

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.