PHP 从字符串中检测和转换日期


php detect and convert dates from a string

我想知道我是否可以以某种方式检测字符串中的date并将其转换为standard date format.

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

Company registered on 16 March 2003

Activity between 10 May 2006 an 10 July 2008 - no changes.

现在我想要一个 PHP 函数应用于字符串并获取日期作为YYYY-mm-dd

例:

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

$date = DateExtract($sting2); // output: ['2006-05-10','2008-07-10']

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

$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);

输出:

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这样的完整名称的短代码。

法典:

$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);

结果:

Array
(
    [0] => Array
        (
            [0] => 10 May 2006
            [1] => 10 July 2008
        )
    [1] => Array
        (
            [0] => 10 
            [1] => 10 
        )
    [2] => Array
        (
            [0] => 
            [1] => 
        )
    [3] => Array
        (
            [0] => 20
            [1] => 20
        )
    [4] => Array
        (
            [0] => 06
            [1] => 08
        )
    [5] => Array
        (
            [0] => 
            [1] => 
        )
)

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

在这里演示

棘手。一种方法可能是推断日期总是出现在某些语法单词之后,如您的示例所示,例如"介于"、"在"等。使用这样的单词作为开始锚点,我们将匹配,直到找到我们可以合理地假设是日期字符串的结尾。以下是我整理的内容:

//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,但它有点有效。

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

正则表达式演示


第一种方法:

<?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演示


第二种方法:

<?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演示