douxie5930 2017-07-05 00:51
浏览 73
已采纳

从日期和月份更改位置将字符串转换为日期

I am working on following issue in PHP. I have these variables:

$fecha = "01-07-2017";

$hora = "11:30";

Now I want to create a date variable with format Y-m-d H:i.

For that I am using following code:

$newDate = date("Y-m-d H:i", strtotime($fecha.' '.$hora));

But I am getting the output:

echo $newDate = 2017-01-07 11:30

I need it to be: 2017-07-01 11:30

What am I doing wrong?

  • 写回答

2条回答 默认 最新

  • douyong6589 2017-07-05 00:58
    关注

    Use the DateTime API to parse your string(s) initially

    $dt = DateTime::createFromFormat('d-m-Y H:i', $fecha.' '.$hora);
    $newDt = $dt->format('Y-m-d H:i');
    

    Demo ~ https://eval.in/827662

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?