duandi8544 2015-09-10 01:22
浏览 45

将特定格式的日期插入MySQL db

I need to insert the current date in the following format into a TIMESTAMP column in a MySQL db: d-m-Y

As of now I am using SQL NOW(), which returns the date as Y-m-d. Because I am using AJAX to display the data I cannot format the returned result using $date_returned->format(d-m-Y). Therefore I need to insert the date in the format that I will display on my AJAX call.

I tried to insert the date using the following functions:

 1) date('d-m-Y');    
 2) (new \DateTime())->format('Y-m-d');

I understand these two functions do pretty much the same thing but I was not sure what else I should try.

MySQL threw the following error for both dates:

Error : (1292) Incorrect datetime value: '-2014' for column 'msg_date' at row 1

I am guessing this should be an easy fix but I can't figure out what is wrong. I tried both TIMESTAMP and DATETIME on MySQL's end but neither worked. (I need it to be TIMESTAMP though).

Any suggestion is welcome!

  • 写回答

3条回答 默认 最新

  • douya6229 2015-09-10 01:58
    关注
    $newdate= date('Y-m-d', strtotime('10-09-2015'));

    or if you want current time just use

    $now = date('Y-m-d');
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)