【问题标题】:php detect and convert dates from a stringphp检测并转换字符串中的日期
【发布时间】:2016-07-02 06:07:14
【问题描述】:

我想知道是否可以以某种方式检测字符串中的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']

【问题讨论】:

  • 您可以使用正则表达式获取所有日期,然后循环匹配并使用 DateTime 将它们转换为您想要的格式。
  • 他要问的是“获取所有日期”:)
  • 我的第一个想法是使用Regex,但当刺痛包含仅number 而不是完整的Date 并且date 之间的空格不止一次时,它也会失败那么正则表达式也将不起作用。它也提供了这样的结果。
  • 如果您有不完整日期的字符串,您希望如何实现?以编程方式不可能知道什么数字是日期,因为没有人可以解释。

标签: php regex string date


【解决方案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

【讨论】:

    【解决方案2】:

    对于查找前两位数字 Date 正则表达式将是 - (?&lt;![0-9])[0-9]{2}(?![0-9]) 这也适用于 Year 的四位数字,对于月份,您可以使用硬编码的 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);
    
        ?>
    

    输出:-

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

    结果 -

    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] => 
            )
    
    )
    

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

    Demo Here

    拉胡尔·丹巴雷

    【讨论】:

      【解决方案3】:

      棘手。一种方法可能是推断日期总是出现在某些语法词之后,如您的示例所示,例如“between”、“on”等。使用这样的词作为开始锚点,然后我们将匹配,直到我们找到我们可以合理假设为日期字符串的结尾。这是我一起破解的:

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

      【讨论】:

      • 我喜欢日期前缀的想法
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-23
      • 1970-01-01
      • 1970-01-01
      • 2012-05-07
      • 2012-05-27
      • 1970-01-01
      相关资源
      最近更新 更多