Convert string to mysql datetime with AM/PM

I am working on a WordPress plugin that allows the user to select a time using a jQuery dropdown script. The time is a string in the format of “5 : 34 PM”. I need to save that value to my mysql database in the datetime format.

So far, I can save the date and time but the AM/PM is not being factored in.

Read More

Here is my PHP function:

function db_tables_insert() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'tweettweet';
    $tweet = $_POST["tweet"];
    $time = $_POST["timepicker"];
    $time=preg_replace('/s+/', '', $time);
    $date = "2015-02-08:";
    $datetime = $date.$time;

    $wpdb->insert( 
        $table_name, 
        array( 
            'time' => $datetime,
            'text' => $tweet, 
        ) 
    );
}

In this example, the value saved to the database would be “2015-02-08 12:13:00” (assuming the user selected 12:13 for the time). The problem is, that value is the same whether the user selects 12:13 am or 12:13 pm.

I need a way to convert the string so that the “am/pm” is taken into consideration when saved to the database.

Related posts

Leave a Reply

1 comment