duanfu5239
2013-05-30 05:35
浏览 225
已采纳

在php中将dd-mm-yyyy转换为yyyy-mm-dd

I tried the following code to change the dateformat from dmy to ymd, but when using i got wrong dates.

My code

$sdate11=date("Y-m-d", strtotime($_POST["txtstartdates"]) );
$sdate111=date("Y-m-d", strtotime($_POST["txtenddates"]) );

dates inserted were

30-05-2013 and  31-05-2013

the date it returned was

2035-11-03 and 2036-11-02 

could you please help me to find what was the problem here and solve it

Thank you.

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

我尝试使用以下代码将dateformat从dmy更改为ymd,但在使用时我的日期错误。

我的代码

  $ sdate11 = date(“Ymd”,strtotime($ _ POST [“txtstartdates”])); 
 $ sdate111  = date(“Ymd”,strtotime($ _ POST [“txtenddates”])); 
   
 
 

插入日期

   30-05-2013 and 31-05-2013 
   
 
 

返回的日期是

   2035-11-03和2036-11-02 
   
 
 

您可以帮我找一下这里的问题并解决它吗 \ n

谢谢。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • dongyuchen9276 2013-05-30 05:39
    已采纳

    Try with split like

    $a = split('-',$_POST["txtstartdates"]);
    

    or you can use explode even like

    $a = explode('-',$_POST["txtstartdates"]);
    $my_new_date = $a[2].'-'.$a[1].'-'.$a[0];
    

    Here strtotime will not work for the format dd-mm-yyyy

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doushao6874 2013-05-30 05:38

    Can't you use the DateTime object to convert the date into the format you want?

    $DateTime = new DateTime($_POST['FIELD']);
    echo $DateTime->format('Y-m-d'); 
    
    评论
    解决 无用
    打赏 举报
  • douqiao3453 2013-05-30 05:38

    You might use DateTime for that:

    $date = DateTime::createFromFormat('d-m-Y', $_POST['txtstartdates']);
    echo $date->format('Y-m-d');
    
    $date = DateTime::createFromFormat('d-m-Y', $_POST['txtenddates']);
    echo $date->format('Y-m-d');
    
    评论
    解决 无用
    打赏 举报
  • dtrvzd1171 2013-05-30 05:51

    Your code seems correct , I dont know why its not working for you Can you try below I have made the '' from "" only

    $sdate11=date('Y-m-d', strtotime($_POST['txtstartdates']));
    $sdate111=date('Y-m-d', strtotime($_POST['txtenddates']));
    
    评论
    解决 无用
    打赏 举报
  • doutuobao9736 2016-12-11 19:41

    You can do it using single line as:

    $show_date = DateTime::createFromFormat('d-m-Y', $dateInput)->format('Y-m-d');
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题