php检测并转换字符串中的日期

时间:2016-03-16 11:27:17

标签: php regex string date

我想知道我是否能以某种方式检测字符串中的plot ... with labels palette 并将其转换为date

让我们考虑下面的输入字符串:

standard date format

Company registered on 16 March 2003

现在我想要一个PHP函数应用于字符串并将日期作为Activity between 10 May 2006 an 10 July 2008 - no changes.

示例:

YYYY-mm-dd

$date = DateExtract($sting1); // output: 2003-03-16

3 个答案:

答案 0 :(得分:3)

要查找前两位数字Date正则表达式为 - (?<![0-9])[0-9]{2}(?![0-9])这也可以应用于Year的四位数,而对于月份,您可以使用硬编码{{1}代码。

string search

输出: -

 <?php

    $string = "Activity between 10 May 2006 an 10 July 2008 - no changes.";

    preg_match_all('/(\d{1,2}) (\w+) (\d{4})/', $string, $matches);
    print_r($matches);

    ?>

更新

要在字符串中查找完成日期,您可以使用此< - strong>

  

适用于像 Array ( [0] => Array ( [0] => 10 May 2006 [1] => 10 July 2008 ) [1] => Array ( [0] => 10 [1] => 10 ) [2] => Array ( [0] => May [1] => July ) [3] => Array ( [0] => 2006 [1] => 2008 ) ) 这样的简短代码和Jan这样的完整名称。

代码 -

January

结果 -

<?php 
$string = "Activity between 10 May 2006 an 10 July 2008 - no changes.";

 preg_match_all('/(\b\d{1,2}\D{0,3})?\b(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|(Nov|Dec)(?:ember)?)\D?(\d{1,2}\D?)?\D?((19[7-9]\d|20\d{2})|\d{2})/', $string, $complete);
    print_r($complete);
?>

因此,您可以在此处获取完整的日期表单并将其转换为标准日期格式。

Demo Here

Rahul Dambare

答案 1 :(得分:1)

棘手。一种方法可能是推断日期总是出现在某些语法词之后,如示例所示,例如: &#34;在&#34;,&#34;之间&#34;使用这样的单词作为开始锚点,我们将匹配,直到我们找到我们可以合理地假设为日期字符串的结尾。这是我一起攻击的内容:

//some strings
$strs = [
    "Company was in business between 14 March 2008 and 21 November 2012 inclusive",
    "I was born on 29 May 1980, 17:37 - it was a Thursday",
    "The big bang did not occur at 2pm, 14 Jun 1971, that's for sure."
];

//container to store possible date matches from strings
$possible_dates = array();

//prep months - long and short forms, to be used in matching
$date_prefix_words = array('between', 'on', 'at', 'during', 'and');
$months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
$months_short = array_map(function($month) { return substr($month, 0, 3); }, $months);

//iterate over and search strings - convert times like 2pm to 14:00, as, if they appear before the date, e.g. string 3, it doesn't get parsed
foreach($strs as $str) {
    $str = preg_replace_callback('/\b\d{1,2}[ap]m\b/', function($time) { return date('H:i', strtotime($time[0])); }, $str);
    preg_match_all('/(?<=\b'.implode('\b( |:)|\b', $date_prefix_words).'\b( |:))(\d|am|pm| |,|\'|:|'.implode('|', $months).'|'.implode('|', $months_short).')+/i', $str, $matches);
    if (count($matches)) $possible_dates = array_merge($possible_dates, $matches[0]);
}

//output before and after results
foreach($possible_dates as &$pd) {
    $pd = preg_replace('/, ?$/', '', $pd);
    echo '<p>Before: '.$pd.'<br />After: '.date('Y-m-d', strtotime($pd)).'</p>';
}

显然,我对您的日期格式做出了某些假设,您可能需要调整REGEX,但它有点有效。

答案 2 :(得分:1)

首先,你必须分别从字符串中提取日期的所有部分。

RegEx Demo

第一种方法:

<?php

function standard_date_format($str) {
    preg_match_all('/(\d{1,2}) (\w+) (\d{4})/', $str, $matches);
    foreach ( $matches[1] as $day   ) { $days  [] = $day;   }
    foreach ( $matches[2] as $month ) { $months[] = $month; }
    foreach ( $matches[3] as $year  ) { $years [] = $year;  }

    $all_months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

    for ($i = sizeof ($days) - 1; $i >= 0; $i--) {
        $month     = array_search ($months[$i], $all_months) + 1;
        $month     = strlen ($month) < 2 ? '0'.$month : $month; 
        $results[] = $years[$i] . '-' . $month . '-' . $days[$i];
    }
    return  $results;
}

$str1 = "Company registered on 16 March 2003";
$str2 = "Activity between 10 May 2006 an 10 July 2008 - no changes.";

print_r(standard_date_format($str1)); // output: 2003-03-16
print_r(standard_date_format($str2)); // output: ['2006-05-10','2008-07-10']

PHP Demo

第二种方法:

<?php

function standard_date_format($str) {
    preg_match_all('/(\d{1,2}) (\w+) (\d{4})/', $str, $matches);
    $dates  = array_map("strtotime", $matches[0]);
    $result = array_map(function($v) {return date("Y-m-d", $v); }, $dates);
    return $result;
}

$str1 = "Company registered on 16 March 2003";
$str2 = "Activity between 10 May 2006 an 10 July 2008 - no changes.";

print_r(standard_date_format($str1)); // output: 2003-03-16
print_r(standard_date_format($str2)); // output: ['2006-05-10','2008-07-10']

PHP Demo