PHP 从表单插入日期时间



>我有一个表单,用户需要在其中选择两个日期,一个是日期,一个是日期和时间。我有一个包含两个字段的基本表单,用户必须在其中输入日期(如 2014-02-02)和时间日期时间(如2014-02-02 10:20:00),表单验证效果很好。

但是,将值插入数据库时会出现问题。这是我所拥有的:

<?php
class Quote
{
  public $job_deadline = null;
  public $job_dispatchdate = null;
}
public function __construct( $data=array() ) {
    if ( isset( $data['job_deadline'] ) ) $this->job_deadline = (int) $data['job_deadline'];
    if ( isset( $data['job_dispatchdate'] ) ) $this->job_dispatchdate = (int) $data['job_dispatchdate'];
    //if ( isset( $data['job_dispatchdate'] ) ) $this->job_dispatchdate = date('Y-m-d H:i:s',strtotime($data['job_dispatchdate']));
}
public function storeFormValues ( $params ) {
    $this->__construct( $params );
       if ( isset($params['job_deadline']) ) {
      $job_deadline = explode ( '-', $params['job_deadline'] );
      if ( count($job_deadline) == 3 ) {
        list ( $y, $m, $d ) = $job_deadline;
        $this->job_deadline = gmmktime ( 0, 0, 0, $m, $d, $y );
      }
    }
    if ( isset($params['job_dispatchdate']) ) {
        $job_deadline = $params['job_dispatchdate'];
        list ( $y, $m, $d, $h, $i, $s ) = $job_dispatchdate;
        $this->job_dispatchdate = gmmktime ( 0, 0, 0, 0, 0, 0, $y, $m, $d, $h, $i, $s );
    }
    /*
    if ( isset($params['job_dispatchdate']) ) {
        $datetime = date('Y-m-d H:i:s', strtotime($params['job_dispatchdate']));
    }
    */
}
public function insertjob() {
    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    //$datetime = date('Y-m-d H:i:s', strtotime($params['job_dispatchdate']));  USING $datetime inplace of FROM_UNIXTIME(:job_dispatchdate) and removing st for job_dispatchdate
    $sql = "INSERT INTO tbl1 (job_deadline, job_dispatchdate) 
    VALUES (FROM_UNIXTIME(:job_deadline), FROM_UNIXTIME(:job_dispatchdate))";               
    $st = $conn->prepare ( $sql );
    $st->bindValue( ":job_deadline", $this->job_deadline, PDO::PARAM_INT );
    $st->bindValue( ":job_dispatchdate", $this->job_dispatchdate, PDO::PARAM_INT );
    $st->execute();
    $this->job_id = $conn->lastInsertId();
    $inserted_id = $this->id = $conn->lastInsertId();
    $conn = null;
}
?>

job_deadline插入正常,问题出在job_dispatchdate.注释掉的部分是我尝试过的东西,但也是不同的工作。我从来没有插入过,所以该字段显示为NULL或像1970-01-01 00:00:00这样的日期。

谁能帮我插入日期时间。

提前谢谢。

伊恩

---编辑---

<?php
class Quote
{
  public $job_deadline = null;
  public $job_dispatchdate = null;
}
public function __construct( $data=array() ) {
    if ( isset( $data['job_deadline'] ) ) $this->job_deadline = (int) $data['job_deadline'];
    if ( isset( $data['job_dispatchdate'] ) ) $this->job_dispatchdate = preg_replace ( "/[^.,-_'|+#"@%?!&:;£$/\(n) a-zA-Z0-9()]/", "", $data['job_dispatchdate'] );
}
public function storeFormValues ( $params ) {
    $this->__construct( $params );
       if ( isset($params['job_deadline']) ) {
      $job_deadline = explode ( '-', $params['job_deadline'] );
      if ( count($job_deadline) == 3 ) {
        list ( $y, $m, $d ) = $job_deadline;
        $this->job_deadline = gmmktime ( 0, 0, 0, $m, $d, $y );
      }
    }
    if ( isset($params['job_dispatchdate']) ) {
        $job_deadline = $params['job_dispatchdate'];
        list ( $y, $m, $d, $h, $i, $s ) = $job_dispatchdate;
        $this->job_dispatchdate = gmmktime ( 0, 0, 0, 0, 0, 0, $y, $m, $d, $h, $i, $s );
    }
}
public function insertjob() {
    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    //$datetime = date('Y-m-d H:i:s', strtotime($params['job_dispatchdate']));  USING $datetime inplace of FROM_UNIXTIME(:job_dispatchdate) and removing st for job_dispatchdate
    $sql = "INSERT INTO tbl1 (job_deadline, job_dispatchdate) 
    VALUES (FROM_UNIXTIME(:job_deadline), FROM_UNIXTIME(:job_dispatchdate))";               
    $st = $conn->prepare ( $sql );
    $st->bindValue( ":job_deadline", $this->job_deadline, PDO::PARAM_INT );
    $st->bindValue( ":job_dispatchdate", $this->job_dispatchdate, PDO::PARAM_STR );
    $st->execute();
    $this->job_id = $conn->lastInsertId();
    $inserted_id = $this->id = $conn->lastInsertId();
    $conn = null;
}
?>

根据注释,我已将代码更改为此。 并将数据库中的字段设置为字符串 Varchar,但无济于事。

 $st->bindValue( ":job_dispatchdate", $this->job_dispatchdate, PDO::PARAM_INT)

应该是

 $st->bindValue( ":job_dispatchdate", $this->job_dispatchdate, PDO::PARAM_STR)

建议:最好将所有日期以 unix 时间戳格式存储在 mysql 中。

编辑 2:如果 INT 类型为job_dispatche列类型,请将它更改为字符串

if ( isset($params['job_dispatchdate']) ) {
        $job_deadline = $params['job_dispatchdate'];
        $this->job_dispatchdate = date('Y-m-d H:i:s', strtotime( $job_deadline ));
}

这似乎解决了它。谢谢大家

相关内容

  • 没有找到相关文章

最新更新