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

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.

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

我的html页面中有一个输入字段,它从jQuery datepicker获得 Date 。< / p>

格式如下所示:

  18/02/2017 
   
 
 <  p>我有一个MYSQL字段,它是 DATETIME   
 
 

我需要将上面的Date插入到这个MYSQL字段中。

所以我尝试了这段代码:

  $ mysqlDate = date('Ymd H:i:s',strtotime($ _ POST ['php_date'])); 
 <  / code>  
 
 

$ mysqlDate 的结果是:

  1970-01-01 01  :00:00 
   
 
 

有人可以就此问题提出建议吗?

提前致谢。

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

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'));
    
    打赏 评论
  • doucigua0449 2017-02-18 13:21

    You have to convert the posted date to a

    YYYY-MM-DD
    

    Format before converting it into date time.

    You can do

    $date=$_POST['php_date'];
    list($day,$month,$year)=explode('/', $date);
    $date=$year.'-'.$month.'-'.$day;
    

    And then use $date.

    打赏 评论

相关推荐 更多相似问题