duaiwo9093
2015-09-04 16:23
浏览 95

使用mysqli保存时间戳

I'm having a problem by saving a jQuery UI datepicker date into a TIMESTAMP column via MySQLi.

My variable $_POST['schedule'] contains 04-09-2015 (dd-mm-yy). I use the following prepared statement:

$stmt = $mysqli->prepare("INSERT INTO campaigns (schedule) VALUES (?)");
$stmt->bind_param("s", strtotime($_POST['schedule']));

When I check the database, the corresponding timestamp column always resets to 0000-00-00 00:00:00. If I echo strtotime($_POST['schedule']) I get a valid UNIX timestamp.

Also changing the bind_param from s to i doesn't works.

I feel like I'm doing a trivial mistake but can't understand what it is. What am I doing wrong?

图片转代码服务由CSDN问答提供 功能建议

通过MySQLi将jQuery UI datepicker日期保存到TIMESTAMP列中,我遇到了问题。

我的变量 $ _ POST ['schedule'] 包含 04-09-2015 (dd-mm-yy)。 我使用以下准备好的声明:

  $ stmt = $ mysqli-> prepare(“INSERT INTO campaign(schedule)VALUES(?)”); 
 $ stmt-  > bind_param(“s”,strtotime($ _ POST ['schedule'])); 
   
 
 

当我检查数据库时,相应的时间戳列始终重置为 0000-00-00 00:00:00 。 如果我回显 strtotime($ _ POST ['schedule']),我会得到一个有效的UNIX时间戳。

同时从 s i 不起作用。

我觉得我犯的是一个微不足道的错误但却无法理解它是什么。 我做错了什么?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dsk61780 2015-09-04 16:52
    已采纳

    The only way I found to fix this is to use the following and change the format:

    $date = DateTime::createFromFormat('d-m-Y', $_POST['schedule']);
    $date = $date->format('Y-m-d');
    $stmt = $mysqli->prepare("INSERT INTO campaigns (schedule) VALUES (?)");
    $stmt->bind_param("s", $date);
    
    已采纳该答案
    打赏 评论
  • dpv46227 2015-09-04 17:03
        $date = date_create($_POST['schedule']);
        $stmt = $mysqli->prepare("INSERT INTO campaigns (schedule) VALUES (?)");
        $stmt->bind_param("s", date_format($date, 'Y-m-d H:i:s'));
    

    Try this. Change the format.

    打赏 评论

相关推荐 更多相似问题