日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php 日期时间 取日期,从PHP中的文本中提取日期,时间和日期范围

發布時間:2023/12/10 php 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php 日期时间 取日期,从PHP中的文本中提取日期,时间和日期范围 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我正在構建一個本地事件日歷,它采用RSS提要和網站抓取并從中提取事件日期.

我之前已經問過如何從PHP here中的文本中提取日期,并在MarcDefiant時獲得了一個很好的答案:

function parse_date_tokens($tokens) {

# only try to extract a date if we have 2 or more tokens

if(!is_array($tokens) || count($tokens) < 2) return false;

return strtotime(implode(" ", $tokens));

}

function extract_dates($text) {

static $patterns = Array(

'/^[0-9]+(st|nd|rd|th|)?$/i', # day

'/^(Jan(uary)?|Feb(ruary)?|Mar(ch)?|etc)$/i', # month

'/^20[0-9]{2}$/', # year

'/^of$/' #words

);

# defines which of the above patterns aren't actually part of a date

static $drop_patterns = Array(

false,

false,

false,

true

);

$tokens = Array();

$result = Array();

$text = str_word_count($text, 1, '0123456789'); # get all words in text

# iterate words and search for matching patterns

foreach($text as $word) {

$found = false;

foreach($patterns as $key => $pattern) {

if(preg_match($pattern, $word)) {

if(!$drop_patterns[$key]) {

$tokens[] = $word;

}

$found = true;

break;

}

}

if(!$found) {

$result[] = parse_date_tokens($tokens);

$tokens = Array();

}

}

$result[] = parse_date_tokens($tokens);

return array_filter($result);

}

# test

$texts = Array(

"The focus of the seminar, on Saturday 2nd February 2013 will be [...]",

"Valentines Special @ The Radisson, Feb 14th",

"On Friday the 15th of February, a special Hollywood themed [...]",

"Symposium on Childhood Play on Friday, February 8th",

"Hosting a craft workshop March 9th - 11th in the old [...]"

);

$dates = extract_dates(implode(" ", $texts));

echo "Dates: \n";

foreach($dates as $date) {

echo " " . date('d.m.Y H:i:s', $date) . "\n";

}

但是,該解決方案有一些缺點 – 首先,它無法匹配日期范圍.

我現在正在尋找一種更復雜的解決方案,可以從示例文本中提取日期,時間和日期范圍.

這是最好的方法嗎?看起來我正在靠回一系列正則表達式語句,一個接一個地運行以捕獲這些情況.我無法看到更好的方法來捕捉日期范圍,但我知道必須有更好的方法來做到這一點.是否有任何庫只用于PHP中的日期解析?

根據要求,日期/日期范圍樣本

$dates = [

" Saturday 28th December",

"2013/2014",

"Friday 10th of January",

"Thursday 19th December",

" on Sunday the 15th December at 1 p.m",

"On Saturday December 14th ",

"On Saturday December 21st at 7.30pm",

"Saturday, March 21st, 9.30 a.m.",

"Jan-April 2014",

"January 21st - Jan 24th 2014",

"Dec 30th - Jan 3rd, 2014",

"February 14th-16th, 2014",

"Mon 14 - Wed 16 April, 12 - 2pm",

"Sun 13 April, 8pm",

"Mon 21 - Wed 23 April",

"Friday 25 April, 10 – 3pm",

"The focus of the seminar, on Saturday 2nd February 2013 will be [...]",

"Valentines Special @ The Radisson, Feb 14th",

"On Friday the 15th of February, a special Hollywood themed [...]",

"Symposium on Childhood Play on Friday, February 8th",

"Hosting a craft workshop March 9th - 11th in the old [...]"

];

我目前正在使用的功能(不是上述功能)大約90%準確.它可以捕獲日期范圍,但如果還指定了時間則有困難.它使用正則表達式列表,非常復雜.

更新:2014年1月6日

我正在研究執行此操作的代碼,使用一系列正則表達式的原始方法依次運行.我認為我接近一個可以從一段文本中提取幾乎任何日期/時間范圍/格式的工作解決方案.當我完成后,我會在這里發布它作為答案.

總結

以上是生活随笔為你收集整理的php 日期时间 取日期,从PHP中的文本中提取日期,时间和日期范围的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。