Converting string to Date and DateTime

后端 未结 10 1620
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 17:07

If I have a PHP string in the format of mm-dd-YYYY (for example, 10-16-2003), how do I properly convert that to a Date and then a DateTime

10条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 17:33

    You need to be careful with m/d/Y and m-d-Y formats. PHP considers / to mean m/d/Y and - to mean d-m-Y. I would explicitly describe the input format in this case:

    $ymd = DateTime::createFromFormat('m-d-Y', '10-16-2003')->format('Y-m-d');
    

    That way you are not at the whims of a certain interpretation.

提交回复
热议问题