格式化日期格式php


Format date format php

我使用的是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

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

如果文本上仍然没有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

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

希望能有所帮助。

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
 ?>

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