开发者

Identify date-related text in a longer message

I'm currently writing a script that would extract all the dates from a messag开发者_开发技巧e and convert them to timestamps. PHP's strtotime (similar to Unix's date -c 'some date') would be perfect for this, as it recognizes all kinds of dates, such as:

  • 5pm today
  • 2010-11-15 16:30
  • Thursday 8:00

However, I'm having trouble finding those dates in the first place. For example, in the following string,

I'll be there for dinner tomorrow at 9:00pm

I need to isolate "tomorrow at 9:00pm", as that's the part that strtotime recognizes.

Is there a regular expression or something similar that would return me all dates that can be parsed by strtotime?


The only thing I can think of is date_parse. A regular expression that matches any format accepted by strtotime would be huge.

An example of date_parse:

$str = "I'll be there for dinner tomorrow at 9:00pm";
$parsed = date_parse($str);
print_r($parsed);

It would output something like this (I removed the unimportant parts from it to make it the result lighter):

Array
(
    [year] => 
    [month] => 
    [day] => 
    [hour] => 21               // 9:00pm
    [minute] => 0              // 9:00pm  
    [second] => 0              // 9:00pm
    [fraction] => 0
    [warning_count] => 1
    [is_localtime] => 1
    [zone_type] => 2
    [zone] => -540
    [is_dst] => 
    [tz_abbr] => I
    [relative] => Array
        (
            [year] => 0
            [month] => 0
            [day] => 1          // tomorrow (would be -1 for yesterday, etc.)
            [hour] => 0
            [minute] => 0
            [second] => 0
        )

)

Whether this works for you depends primarily on what your input looks like. If you have more than one instance of a date in your input string, it will not work as expected.


This might not be totally efficient, but should work for any date string that consists of up to 5 words in length. I would write the function, but I think you'll get the idea with the comments below...

$words = explode(' ',$original_string);

// Use the array_chunk() function break up this array into 1-word,
// 2-word, 3-word, and 4-word long substrings of the original string

// Reform them back into strings and pass each one through strtodate()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜