【问题标题】:Format date format php格式化日期格式php
【发布时间】:2014-10-27 07:59:20
【问题描述】:

我正在使用 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

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

【问题讨论】:

  • 提示:提交表单
  • 我把它放到我的 php 代码中,但问题仍然是如何将此日期格式格式化为 datetime mysql 值

标签: php html date datetime


【解决方案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

【讨论】:

  • @EMIN 很高兴这有帮助
【解决方案2】:

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)
}
*/

希望对你有帮助。

【讨论】:

    【解决方案3】:

    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

    【讨论】:

      猜你喜欢
      • 2012-07-24
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多