dsb0003795 2017-02-18 13:13
浏览 114
已采纳

PHP:将日期转换为datetime并将其插入到mysql数据库中?

I have an input field in my html page that gets Date from jQuery datepicker.

The format looks like this:

18/02/2017

I have a MYSQL field which is DATETIME

I need to insert the Date above into this MYSQL field.

So i tried this code:

$mysqlDate = date('Y-m-d H:i:s', strtotime($_POST['php_date']));

but the result of $mysqlDate is this:

1970-01-01 01:00:00

Could someone please advise on this issue?

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dqfr90979 2017-02-18 13:17
    关注

    Do one thing,

    $mysqlDate = date('Y-m-d H:i:s', strtotime(str_replace("/","-",$_POST['php_date'])));
    echo $mysqlDate;
    

    Give it a try,

    it should work.

    Note: Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d.

    Source link.

    Your new concern answer,

    $mysqlDate = date('Y-m-d H:i:s', strtotime(str_replace("/","-",$_POST['php_date']).' + '.rand(30,60*60*24*3).' seconds'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效