格式化日期格式php

时间:2014-10-27 07:59:21

标签: php html date datetime

我正在使用DateTimePicker。

<div class="input-group date form_datetime">
    <input type="text" size="16" readonly class="form-control">
    <span class="input-group-btn">
        <button class="btn btn-success date-set" type="button"><i class="fa fa-calendar</i></button>
    </span>
</div>

它使用以下格式返回日期/时间:16 October 2014 - 14:50

但我的问题是我如何格式化这种格式并将其存储到我的数据库中。

3 个答案:

答案 0 :(得分:1)

date_parse来救援:

php> $a = '16 October 2014 - 14:50'
php> var_dump(date_parse($a))
/* ⇒
array(12) {
  'year' =>
  int(2014)
  'month' =>
  int(10)
  'day' =>
  int(16)
  'hour' =>
  int(14)
  'minute' =>
  int(50)
  'second' =>
  int(0)
  'fraction' =>
  double(0)
  'warning_count' =>
  int(0)
  'warnings' =>
  array(0) {
  }
  'error_count' =>
  int(1)
  'errors' =>
  array(1) {
    [16] =>
    string(20) "Unexpected character"
  }
  'is_localtime' =>
  bool(false)
}
*/

希望它有所帮助。

答案 1 :(得分:1)

如果您仍未在文字上设置name=""属性,请善意:

<input name="date" type="text" size="16" readonly class="form-control" />

然后在PHP中:

$date = $_POST['date']; // form input

$datetime_object = DateTime::createFromFormat('d F Y - H:i', $date); // feed the proper format
$insert_date = $datetime_object->format('Y-m-d H:i:s'); // datetime format

// the rest of insertion is up to you. Either use MYSQLI or PDO

答案 2 :(得分:0)

Mysql的日期格式为YYYY-MM-DD,因此使用日期功能非常简单。 我们走了......

<?php
 $dateTime='6 October 2014 - 14:50';
 list($date,$time)=explode(' - ',$dateTime);
 echo date('Y-m-d H:i:s', strtotime($date.' '.$time));
 // code here
 ?>

请记住在db字段中存储该值,其数据类型为DATETIME

相关问题