我有一个'23/05/2013'的字符串,我想从中创建一个新的Date Time对象,所以我做了:
new \DateTime('23/05/2013');
Run Code Online (Sandbox Code Playgroud)
知道为什么我一直都会收到这个错误:
DateTime::__construct(): Failed to parse time string (23/05/2013) at position 0 (2): Unexpected character
Run Code Online (Sandbox Code Playgroud)
cra*_*231 57
根据http://www.php.net/manual/en/datetime.formats.date.php
这是mm/dd/yyyy,这是美国人,而不是英国人
使用
DateTime::createFromFormat('d/m/Y', '23/05/2013');
Run Code Online (Sandbox Code Playgroud)
Far*_*kie 30
如果你想正常使用该对象而不是静态地尝试这个:
$datetime = new DateTime();
$newDate = $datetime->createFromFormat('d/m/Y', '23/05/2013');
Run Code Online (Sandbox Code Playgroud)
然后你可以像平常一样使用它:
echo $newDate->format('Y-m-d');
Run Code Online (Sandbox Code Playgroud)