beginner beginner - 7 days ago 5
Javascript Question

Convert JavaScript type time to MySQL time

In my HTML I have an input type

time
, and I want to insert this value in a column type "time" in MySQL using PHP. How can I do this?

<label class="item item-input">
<span class="input-label">Heure de d├ępart </span>
<input type="time" placeholder="hh:mm" ng-model="heureDepart"><!-- datetime -->
</label>


The time looks like
Thu Jan 01 1970 10:00:00 GMT+0000 (Maroc)
in HTML. I just want to save the value
(10:00:00)
in MySQL because I have a column type "time" in my database.

Answer

You could try this, basically using the DataTime class in PHP will allow you to convert pretty much any datetime format into any other like so

<?php
$str = 'Thu Jan 01 1970 10:11:12 GMT+0000 (Maroc)';
try {
    $date = new DateTime($str);
} catch (Exception $e) {
    echo $e->getMessage();
    exit(1);
}

echo $date->format('H:i:s');
?>