dov11020 2015-02-27 16:37
浏览 13
已采纳

SQL,PHP:转换日期格式[重复]

This question already has an answer here:

I'm having a bit of a problem where both the SQL function and the php function does not convert dates properly when they're in the format of "DD-MM-YYYY". I am using a mysql database and I want to convert the date to "YYYY-MM-DD" before it is entered into the database.

The SQL function:

INSERT INTO lease 
VALUES(3, 4, STR_TO_DATE('22-02-2015', '%Y-%m-%d'),    STR_TO_DATE('27-02-2015', '%Y-%m-%d'))

Returns as "2022-02-20" and "2027-02-20"

Also, the php function puts the month and day in the wrong place so I have to do "Y-d-m" instead, like so:

$startdate = date("Y-d-m", strtotime('27-02-2015'));
$enddate = date("Y-d-m", strtotime('23-02-2015'));

Although that stores it correctly. Any date where the day is > 12 will reset the date to"1970-01-01" because it thinks the day is the month.

Can anyone help with this?

</div>
  • 写回答

1条回答 默认 最新

  • dpdhsq0783 2015-02-27 16:40
    关注

    The format argument for STR_TO_DATE() is the format used for the string argument

    So use

    STR_TO_DATE('22-02-2015', '%d-%m-%Y'),    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥15 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)