Creating new Date Time from string

前端 未结 2 873
醉梦人生
醉梦人生 2020-12-09 14:59

I have a string which is \'23/05/2013\' and I wanted to create a new Date Time object from this, so I did:

new \\DateTime(\'23/05/2013\');

相关标签:
2条回答
  • 2020-12-09 16:00

    If you want to use the object normally rather than statically try this:

    $datetime = new DateTime();
    $newDate = $datetime->createFromFormat('d/m/Y', '23/05/2013');
    

    then you can use it like normal:

    echo $newDate->format('Y-m-d');
    
    0 讨论(0)
  • 2020-12-09 16:01

    According to http://www.php.net/manual/en/datetime.formats.date.php

    It's mm/dd/yyyy, which is American, not British

    Use

    DateTime::createFromFormat('d/m/Y', '23/05/2013');
    
    0 讨论(0)
提交回复
热议问题