drxdai15012937753 2012-05-23 17:27
浏览 42
已采纳

php日期时间似乎无法正常[重复]

This question already has an answer here:

I'm now inserting a date/time data into my date/time field in the database. I have tried many techniques such as:

 $date = date("d M Y");
 $date = date("d M Y", mktime());
 $date = getdate();
 $date = date("d M Y", strtotime('now'));

and also this function: date_default_timezone_set('Asia/Bangkok');

but it still does not get the datetime.

This is my sql statement:

$sql = "INSERT INTO ".TABLE_CONTENT."(`title`, `detail`, `crdate`, `modate`, `crby`, moby, isdel) VALUES('".$title."', '".$detail."', '".$crdate."', '".$modate."', '".$crby."', '".$moby."', $isdel)";

After performing the query I got only the 0000-00-00 00:00:00 in my database.

Any help or suggestion would be very appreciated.

Thanks

</div>
  • 写回答

1条回答 默认 最新

  • doujizhong8352 2012-05-23 17:29
    关注

    Looks like your database fields are set to timestamp, so you'll have to insert data into the database fields in exactly that format.

    So this would work

    $date = date('Y-m-d H:i:s');
    

    You could also use the MySQL NOW(), which would insert the current timestamp into that field, although the timezone would be set to whatever timezone your server is on.

    example:

    $sql = "INSERT INTO <table> (title, crdate) VALUES ('test', NOW())";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题