从字符串中提取日期时间

时间:2015-06-10 14:13:47

标签: php regex string

我有字符串:

"  between Fri Jun 09 04:44:43 UTC 2000 and Sun Apr 20 10:40:41 UTC 2003 "

我想提取:

"Fri Jun 09 04:44:43 UTC 2000"

请检查以下代码。

echo $date_string = "  between Fri Jun 09 04:44:43 UTC 2000 and Sun Apr 20 10:40:41 UTC 2003 ";

$regex = "/\between (\s|[a-z]*|[A-Z]*|[0-9]|\W)* \and/";    
preg_match($regex,$date_string,$match);

echo "<pre>";
var_dump($match);
echo "</pre>";

1 个答案:

答案 0 :(得分:2)

使用此

$string = "  between Fri Jun 09 04:44:43 UTC 2000 and Sun Apr 20 10:40:41 UTC 2003 ";
$start = 'between ';
$end = ' and';
$startpos = strpos($string, $start) + strlen($start);

if (strpos($string, $start) !== false) {
   $endpos = strpos($string, $end, $startpos);

   if (strpos($string, $end, $startpos) !== false) {
      $data = substr($string, $startpos, $endpos - $startpos);
   }
}

var_dump($data);