Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php从字符串中检测和转换日期_Php_Regex_String_Date - Fatal编程技术网

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

php从字符串中检测和转换日期,php,regex,string,date,Php,Regex,String,Date,我想知道我是否可以检测字符串中的日期,并将其转换为标准日期格式 让我们考虑下面的输入字符串: 于2003年3月16日注册的公司 或 2006年5月10日至2008年7月10日期间的活动-无变化。 现在我想要一个PHP函数应用于字符串,并将日期设置为YYYY-mm-dd 例如: $date=DateExtract($sting1);//输出:2003-03-16 $date=DateExtract($sting2);//输出:['2006-05-10','2008-07-10']查找前两位数字日期

我想知道我是否可以检测字符串中的
日期
,并将其转换为
标准日期格式

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

于2003年3月16日注册的公司

2006年5月10日至2008年7月10日期间的活动-无变化。

现在我想要一个PHP函数应用于字符串,并将日期设置为
YYYY-mm-dd

例如:

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


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

查找前两位数字
日期
Regexp将为-
(?这也可以应用于
的四位数字,也可以使用硬编码
字符串搜索
代码

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

    ?>
更新 要在字符串中查找完整日期,您可以使用此-

它适用于月份的短代码,如
Jan
,以及完整的名称,如
janur

代码-

<?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);
?>
因此,您可以在这里获取完整的日期表单,并将其转换为标准日期格式


Rahul Dambare

棘手。一种方法可能是推断日期总是出现在某些语法单词之后,如您的示例中所示,例如“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>';
}
//一些字符串
$strs=[
“公司于2008年3月14日至2012年11月21日(含)期间开展业务”,
“我出生于1980年5月29日,17:37——那是一个星期四”,
“大爆炸不是1971年6月14日下午2点发生的,这是肯定的。”
];
//用于存储字符串中可能的日期匹配项的容器
$mablue_dates=array();
//准备月份-长和短表格,用于匹配
$date_prefix_words=数组('between','on','at','during','and');
$months=数组('1月'、'2月'、'3月'、'4月'、'5月'、'6月'、'7月'、'8月'、'9月'、'10月'、'11月'、'12月');
$months_short=array_map(函数($month){return substr($month,0,3);},$months);
//迭代并搜索字符串-将时间(如下午2点)转换为14:00,因为如果它们出现在日期之前,例如字符串3,则不会对其进行解析
foreach($str作为$str){
$str=preg_replace_回调('/\b\d{1,2}[ap]m\b/',函数($time){返回日期('H:i',strtotime($time[0]);},$str);

preg_match_all('/(?首先,必须分别从字符串中提取日期的所有部分


第一种方法:

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

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']

您可以使用regex获取所有日期,然后循环匹配项,并使用DateTime将其转换为所需格式。他问的是“获取所有日期”:我的第一个想法是使用
Regex
,但是当一个sting只包含
数字
而不是一个完整的
日期
时,它也会失败,如果
Date
之间的空格不止一次,那么regexp也不会工作。它也会提供这样的结果。如果你有日期不完整的字符串,你希望如何实现伊芙?不可能通过编程知道什么数字是日期,因为没有人可以解释。这会帮助你我喜欢日期前缀的想法
<?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

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']