就 PHP 读取和解析日期的方式而言,$date 字符串的当前格式无效 - 有关详细信息,请参阅这两个 URL:
http://php.net/manual/en/function.strtotime.php
http://php.net/manual/en/datetime.formats.php
基本上,当使用斜杠 (/) 作为日期分隔符时,PHP 假定您输入的是 MM/DD/YYYY。如果可能的话,我会看到更新创建该日期字符串的任何输入以将其保存为MM/DD/YYYY 格式-这可能是最好的解决方案。
但是,如果这不是一个选项,根据您提供的内容,一种方法是将16 和2 从 DMY 格式转换为 MDY 格式。这是一个关于如何使用explode() 和字符串连接的示例:
<?php
// The original string you provided, with a date in `DD/MM/YYYY` format
$dateString = "16/2/2014 3:41:01 PM";
// The explode function will let us break the string into 3 parts, separated by the forward slashes. Using your example, these gives us an array containing the following:
// 0 => '16'
// 1 => '2'
// 2 => '2014 3:41:01 PM'
$stringPieces = explode('/', $dateString, 3);
// Piece the above array back together, switching the places of entries 0 and 1 to create a date in the format `MM/DD/YYYY`. This results in:
// 2/16/2014 3:41:01 PM"
$newDateString = $stringPieces[1] . '/' . $stringPieces[0] . '/' . $stringPieces[2];
// Use the reformatted date string in the date() function:
$newDate = date("Y-m-d H:i:s", strtotime($newDateString));
var_dump($newDate);
在我的测试中var_dump()的输出是string(19) "2014-02-16 15:41:01"