使用 strtotime 转换为日期,输出始终为空并带有特殊字符
convert to date with strtotime, the output always empty with special char
我试图在这个日期中剪切特殊字符,当我尝试使用 strtotime 转换为日期时,输出始终为空。但在另一个日期它的工作。为什么?
数据特殊字符:
  11:44:08 AM 10/24/2012 Â
  3:37:43 PM 9/8/2012 Â
  13:21:22 09/01/2017 Â
  0:30:06 13/09/2017 Â
  0:32:00 13/09/2017 Â
日期示例:
11:44:08 AM 10/24/2012
3:37:43 PM 9/8/2012
13:21:22 09/01/2017
0:30:06 13/09/2017
0:32:00 13/09/2017
转换为 strotime :
1351071848
1347111463
1504264882
empty *mean nothing
empty
代码:
$test = preg_replace("/[^0-9a-zA-Z \/:\-]/", "", $date);
$xtime = strtotime($date);
$result = date("Y-m-d H:i:s",$xtime);
最终结果:
2012-10-24 11:44:08
2012-09-08 15:37:43
2017-09-01 13:21:22
1970-01-01 01:00:00
1970-01-01 01:00:00
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/
), then the American m/d/y is assumed; whereas if the separator is a dash (-
) or a dot (.
), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-
), the date string is parsed as y-m-d.
您可以尝试以下逻辑,如果需要可以进一步扩展:
$arr = ['Â Â 11:44:08 AM 10/24/2012Â Â','Â Â 3:37:43 PM 9/8/2012Â Â ','Â Â 13:21:22 09/01/2017Â Â','Â Â 0:30:06 13/09/2017Â Â','Â Â 0:32:00 13/09/2017Â Â'];
foreach ($arr as $date){
$test = preg_replace("~[^0-9a-zA-Z\s/:-]+~u", "", $date);
$result = $test." => ";
if (($xtime = strtotime($test)) === false) // European dd/MM/yyyy format?
{
if (($xtime = strtotime(str_replace('/', '-', $test))) === false) {
$result .= "UNKNOWN FORMAT"; // add more logic here if need be
} else {
$result .= date("Y-m-d H:i:s", $xtime);
}
}
else { // OK, it is MM/dd/yyyy
$result .= date("Y-m-d H:i:s",$xtime);
}
echo $result."\n";
}
输出:
11:44:08 AM 10/24/2012 => 2012-10-24 11:44:08
3:37:43 PM 9/8/2012 => 2012-09-08 15:37:43
13:21:22 09/01/2017 => 2017-09-01 13:21:22
0:30:06 13/09/2017 => 2017-09-13 00:30:06
0:32:00 13/09/2017 => 2017-09-13 00:32:00
我试图在这个日期中剪切特殊字符,当我尝试使用 strtotime 转换为日期时,输出始终为空。但在另一个日期它的工作。为什么?
数据特殊字符:
  11:44:08 AM 10/24/2012 Â
  3:37:43 PM 9/8/2012 Â
  13:21:22 09/01/2017 Â
  0:30:06 13/09/2017 Â
  0:32:00 13/09/2017 Â
日期示例:
11:44:08 AM 10/24/2012
3:37:43 PM 9/8/2012
13:21:22 09/01/2017
0:30:06 13/09/2017
0:32:00 13/09/2017
转换为 strotime :
1351071848
1347111463
1504264882
empty *mean nothing
empty
代码:
$test = preg_replace("/[^0-9a-zA-Z \/:\-]/", "", $date);
$xtime = strtotime($date);
$result = date("Y-m-d H:i:s",$xtime);
最终结果:
2012-10-24 11:44:08
2012-09-08 15:37:43
2017-09-01 13:21:22
1970-01-01 01:00:00
1970-01-01 01:00:00
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (
/
), then the American m/d/y is assumed; whereas if the separator is a dash (-
) or a dot (.
), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-
), the date string is parsed as y-m-d.
您可以尝试以下逻辑,如果需要可以进一步扩展:
$arr = ['Â Â 11:44:08 AM 10/24/2012Â Â','Â Â 3:37:43 PM 9/8/2012Â Â ','Â Â 13:21:22 09/01/2017Â Â','Â Â 0:30:06 13/09/2017Â Â','Â Â 0:32:00 13/09/2017Â Â'];
foreach ($arr as $date){
$test = preg_replace("~[^0-9a-zA-Z\s/:-]+~u", "", $date);
$result = $test." => ";
if (($xtime = strtotime($test)) === false) // European dd/MM/yyyy format?
{
if (($xtime = strtotime(str_replace('/', '-', $test))) === false) {
$result .= "UNKNOWN FORMAT"; // add more logic here if need be
} else {
$result .= date("Y-m-d H:i:s", $xtime);
}
}
else { // OK, it is MM/dd/yyyy
$result .= date("Y-m-d H:i:s",$xtime);
}
echo $result."\n";
}
输出:
11:44:08 AM 10/24/2012 => 2012-10-24 11:44:08
3:37:43 PM 9/8/2012 => 2012-09-08 15:37:43
13:21:22 09/01/2017 => 2017-09-01 13:21:22
0:30:06 13/09/2017 => 2017-09-13 00:30:06
0:32:00 13/09/2017 => 2017-09-13 00:32:00