I am trying to save time into MySQL database. I have kept the column type as time
in database.
From the form I am getting the $_POST
for time in 24-Hr format.
The value from the form for time is as follows:
$_POST[openTime] = 20:00
When I convert the time by using strtotime()
and date()
I get the output as:
$_POST['openTime'] = date( "h:i:s A", strtotime($_POST['openTime']));
Output:
$_POST[openTime] => 08:00:00 PM
So I have a three part question:
Am I doing it in the correct way?
Can I store the values of $_POST
directly into database after the steps I have performed above? If no what processing is needed before I store into the database?
so here after the above changes to $_POST['openTime']
the value is: $_POST[openTime] => 08:00:00 PM
Please note that there will database operations based on time as well. For example: "which all places are open after 8PM?"
MySQL TIME
field type has format like HH:MM:SS
, range from -838:59:59
to 838:59:59
. Read more here . Based on that, you cannot store formated time like 08:00:00 PM
.
If you have posted open time as 20:00
, then you need to use H:i:s
format:
$_POST['openTime'] = date("H:i:s", strtotime($_POST['openTime']));
But IMHO this is unnecessary. What you need is a validation, smth like this:
if (preg_match('~^\d{2}:\d{2}$~', $_POST['openTime'])) {
echo "posted time is valid!";
$saveTime = $_POST['openTime'] . ':00';
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.