English 中文(简体)
PHP对日期进行说明
原标题:Parsing a string for dates in PHP

举例来说,鉴于任意描述(,如果下周五将玩 c”(<>/code>或“Gadzooks”,6月17日是否已有?),你将如何从那里提取日期?

如果看上去像一个好的候选人,可以提出另一种选择。 我希望能够把“推特”信息贴上日期。 我正在研究的是用户在这项服务中指示使用的方法,这样他们就可以使用一种比较容易的格式,不过我喜欢尽可能透明。 你们是否认为有好的中间点?

最佳回答

如果你拥有马力,你可以try以下算法。 我举一个例子,把ted的工作留给你:

//Attempt to perform strtotime() on each contiguous subset of words...

//1st iteration
strtotime("Gadzooks, is it 17th June already")
strtotime("is it 17th June already")
strtotime("it 17th June already")
strtotime("17th June already")
strtotime("June already")
strtotime("already")

//2nd iteration
strtotime("Gadzooks, is it 17th June")
strtotime("is it 17th June")
strtotime("17th June") //date!
strtotime("June") //date!

//3rd iteration
strtotime("Gadzooks, is it 17th")
strtotime("is it 17th")
strtotime("it 17th")
strtotime("17th") //date!

//4th iteration
strtotime("Gadzooks, is it")
//etc

我们可以假设,strtotime ("17th June")strtotime (“17th”)更为准确,因为它含有更多的词语......即“next日星期五”总是比“Friday”更准确。

问题回答

我这样做:

第一次检查,如果整个护卫期为有效日期。 如果是的话,你又做了。

如果不是的话,就确定你在座的字数(例如白天)。 让这一数字为零。

选择每个一字的组合和使用方言,看该短语是否有效。 如果是的话,你会发现你在原体内所显示的最长有效日期。

如果不是的话,则每两字的组合和使用方言,看该短语是否有效。 如果是的话,你会发现你在原体内所显示的最长有效日期。

......并如此,直到你找到一个有效的日期,说明或搜查每一个单词/个人字。 通过找到最长的对应办法,你获得最知情的日期(如果是这样的话)。 由于你重新处理信条,你的说法永远不会是巨大的。

在Juan Cortes利用Doloph s算法的断裂环节的激励下,我走过前面并写了自己。 请注意,我决定先成功回来。

<?php
function extractDatetime($string) {
    if(strtotime($string)) return $string;
    $string = str_replace(array(" at ", " on ", " the "), " ", $string);
    if(strtotime($string)) return $string;

    $list = explode(" ", $string);
    $first_length = count($list);
    for($j=0; $j < $first_length; $j++) {
        $original_length = count($list);
        for($i=0; $i < $original_length; $i++) {
            $temp_list = $list;
            for($k = 0; $k < $i; $k++) unset($temp_list[$k]);
            //echo "<code>".implode(" ", $temp_list)."</code><br/>"; // for visualizing the tests, if you want to see it
            if(strtotime(implode(" ", $temp_list))) return implode(" ", $temp_list);
        }
        array_pop($list);
    }

    return false;
}

Inputs

$array = array(
        "Gadzooks, is it 17th June already",
        "I’m going to play croquet next Friday",
        "Where was the dog yesterday at 6 PM?",
        "Where was Steve on Monday at 7am?"
);

foreach($array as $a) echo "$a => ".extractDatetime(str_replace("?", "", $a))."<hr/>";

Outputs

Gadzooks, is it 17th June already
is it 17th June already
it 17th June already
17th June already
June already
already
Gadzooks, is it 17th June
is it 17th June
it 17th June
17th June
Gadzooks, is it 17th June already => 17th June
-----
I’m going to play croquet next Friday
going to play croquet next Friday
to play croquet next Friday
play croquet next Friday
croquet next Friday
next Friday
I’m going to play croquet next Friday => next Friday
-----
Where was Rav Four yesterday 6 PM
was Rav Four yesterday 6 PM
Rav Four yesterday 6 PM
Four yesterday 6 PM
yesterday 6 PM
Where was the Rav Four yesterday at 6 PM? => yesterday 6 PM
-----
Where was Steve Monday 7am
was Steve Monday 7am
Steve Monday 7am
Monday 7am
Where was Steve on Monday at 7am? => Monday 7am
-----

类似might :

$months = array(
                    "01" => "January", 
                    "02" => "Feberuary", 
                    "03" => "March", 
                    "04" => "April", 
                    "05" => "May", 
                    "06" => "June", 
                    "07" => "July", 
                    "08" => "August", 
                    "09" => "September", 
                    "10" => "October", 
                    "11" => "November", 
                    "12" => "December"
                );

$weekDays = array(
                    "01" => "Monday", 
                    "02" => "Tuesday", 
                    "03" => "Wednesday", 
                    "04" => "Thursday", 
                    "05" => "Friday", 
                    "06" => "Saturday", 
                    "07" => "Sunday"
                );

foreach($months as $value){
    if(strpos(strtolower($string),strtolower($value))){
        \ extract and assign as you like...
    }
}

http://www.un.org/Depts/DGACM/index_french.htm

使用<代码>strtotime php的功能。

当然,你需要制定一些规则来加以禁止,因为你需要删除所有额外内容,但除此之外,它具有非常灵活的职能,将比你在这方面更可能帮助你。

例如,它可以采取像“延长星期五”和“6月15日”这样的扼杀手段,并归还在座标上的适当UNIX时限。 我认为,如果你考虑一些基本规则,如寻找“十大”和周及月名,你就可以这样做。

如果你可以把“next日星期五”从“Immm 开始在下星期五玩 c”,你可以抽出日期。 看像一个 f子项目那样做! 但铭记<代码>strtotime 仅使用english短语,不使用任何其他语文。

例如,确定所有“最长一周”案件的一条规则是简单明了的:

$datestring = "I m going to play croquet next Friday";

$weekdays = array( monday , tuesday , wednesday ,
                   thursday , friday , saturday , sunday );

foreach($weekdays as $weekday){
    if(strpos(strtolower($datestring),"next ".$weekday) !== false){
        echo date("F j, Y, g:i a",strtotime("next ".$weekday));
    }
}

This will return the date of the next weekday mentioned on the string as long as it follows the rule! In this particular case, the output was June 18, 2010, 12:00 am. With a few (maybe more than a few!) of those rules you will more than likely extract the correct date in a high percentage of the cases, considering that the users use correct spelling though.

和大家一样,你经常表示,也很少耐心这样做。 编码中最困难的部分是决定你将如何对待你的问题,而不是在你知道什么时候把它搁置起来。

在<>Dolph Mathews思想之后,我基本上无视我先前的回答,因此,我建立了一种完全是这样的事情。 它把它所认为的描述归为与日期、日期顺序、日期本身或与用户特定格式或预定格式相匹配的日期(j, Y)。 我在号指示。 这里是两个例子的输出方:

Output: Array ( 
           [string] => "next friday",
           [unix] => 1276844400,
           [date] => "June 18, 2010" 
        )

<<>Input:“Gadzooks”, 是6月17日吗?”

Output: Array ( 
           [string] => "17th june",
           [unix] => 1276758000,
           [date] => "June 17, 2010" 
        )

我希望它能帮助某人。

根据<>Dolph s建议,我写了我认为符合目的的职能。

public function parse_date($text, $offset, $length){

  $parseArray = preg_split( "/[s,.]/", $text);
  $dateTest = implode(" ", array_slice($parseArray, $offset, $length == 0 ? null : $length));

  $date = strtotime($dateTest);

  if ($date){
    return $date;
  }

  //make the string one word shorter in the front
  $offset++;

  //have we reached the end of the array?
  if($offset > count($parseArray)){

    //reset the start of the string
    $offset = 0;

    //trim the end by one
    $length--;

    //reached the very bottom with no date found
    if(abs($length) >= count($parseArray)){
      return false;
    }
  }

  //try to find the date with the new substring
  return $this->parse_date($text, $offset, $length);
}

请将此称为:

页: 1 规定法定日期

建议的算法多数事实上是表面的。 我建议对日期使用一些ice,并与它一起测试判决。 举例说:

(d{1,2})? 
((mon|tue|wed|thu|fri|sat|sun)|(monday|tuesday|wednesday|thursday|friday|saturday|sunday))?
(d{1,2})? (d{2,4})?

我 months了几个月,因为我不相信我会按正确的顺序记住他们。

这是最容易的解决办法,但我的工作将优于其他以人才为基础的解决办法。 (安道尔是,它几乎没有一个不受阻挠的管理机构,但你却有点。) 然后在相应的护卫上适用体积功能。 这是最简单和最快的解决办法。





相关问题
Weird Time Issue in Python

Problem with using times in Python. Terminal > Python >>> calendar.timegm(datetime.datetime.now().utctimetuple()) 1258449380 This time indicates GMT: Tue, 17 Nov 2009 09:16:20 GMT Eclipse ...

How does a .NET process get the culture information?

I have a Windows service running (C#, .NET 2.0) on Windows Server 2003 R2. In one server the System.Threading.Thread.CurrentThread.CurrentCulture is {en-AU} and in the other {en-US}. This has caused a ...

Best way to get maximum Date value in java?

I m writing a bit of logic that requires treating null dates as meaning forever in the future (the date in question is an expiration date, which may or may not exist). Instead of putting in special ...

Date format in SQL Server

Short Question: What s the best date format to use in SQL Server? Long Explanation: We re converting our database from mysql to SQL Server. In mysql we always used int(11) to avoid the daylight ...

Converting asp.net/sql web app to be ime zone savy

Our current app stores all dates via the server s datetime. I m thinking we need to update all datetime values in the database over to UTC time. Now for displaying these dates back to the user, I ...

Check if string is of SortableDateTimePattern format

Is there any way I can easily check if a string conforms to the SortableDateTimePattern ("s"), or do I need to write a regular expression? I ve got a form where users can input a copyright date (as a ...

DateTime in PropertyGrid in .Net

I m currently working on some DateTime properties in a PropertyGrid in c#.Net. Im using the default drop-down datetime picker. My question: Is there any way to show the full date with the time? By ...