【问题标题】:English to Time英语到时间
【发布时间】:2010-12-22 00:25:08
【问题描述】:

有谁知道将英文时间表示形式转换为时间戳的好类/库?

目标是转换自然语言短语,例如“十年后”、“三周”和“10 分钟内”,并为它们计算出最佳匹配的 unix 时间戳。

我已经编写了一些非常糟糕且未经测试的代码来继续它,但我确信那里有很棒的日历等解析器。

private function timeparse($timestring)
{
    $candidate = @strtotime($timestring);
    if ($candidate > time()) return $candidate; // Let php have a bash at it

    //$thisyear = date("Y");
    if (strpos($timestring, "min") !== false) // Context is minutes
    {
            $nummins = preg_replace("/\D/", "", $timestring);
            $candidate = @strtotime("now +$nummins minutes");
            return $candidate;
    }

    if (strpos($timestring, "hou") !== false) // Context is hours
    {
            $numhours = preg_replace("/\D/", "", $timestring);
            $candidate = @strtotime("now +$numhours hours");
            return $candidate;
    }

    if (strpos($timestring, "day") !== false) // Context is days
    {
            $numdays = preg_replace("/\D/", "", $timestring);
            $candidate = @strtotime("now +$numdays days");
            return $candidate;
    }

    if (strpos($timestring, "year") !== false) // Context is years (2 years)
    {
            $numyears = preg_replace("/\D/", "", $timestring);
            $candidate = @strtotime("now +$numyears years");
            return $candidate;
    }

    if (strlen($timestring) < 5) // 10th || 2nd (or probably a number)
    {
            $day = preg_replace("/\D/", "", $timestring);
            if ($day > 0)
            {
                    $month = date("m");
                    $year = date("y");
                    return strtotime("$month/$day/$year");
            }
            else
            {
                    return false;
            }
    }

    return false; // No can do.
}

【问题讨论】:

  • 输入“未来三年两天五分钟”怎么样?
  • 是的,就是这样。这就是我需要的。
  • “三年,两天零五分钟”和类似的应该很容易转换为 ISO8601 间隔:“P3Y2DT5M”,您可以将其提供给 DateInterval 并添加到 DateTime 对象。
  • 正则表达式将无法解析这些。你需要的是一个语法解析器(野牛等),这在技术上就像创建一个迷你编译器:P
  • 这是一个有趣的挑战。在我看来,如果有一组用户提交的日期数据来了解您的用户群如何表达这样的日期,那就太好了。

标签: php time timestamp


【解决方案1】:

CocoaGNUStepNSDateFormatter 能够处理这样的时间表示。 GNUStep 版本是开源的。

【讨论】:

  • 考虑到 php 标签,不太可能有很大帮助。怀疑他在寻找可可图书馆
  • 我认为 mouviciel 试图说明的一点是他可以将库移植到 php。
  • 感谢您发布此信息。我很欣赏这个建议,但进入那里有相当大的障碍,我不知道它的效果如何,因此我不愿意花时间。如果找不到其他解决方案,我会看看它。
【解决方案2】:

使用DateTime 类。

例如:

$string='four days ago';
$d=date_create($string);
$d->getTimestamp();

预计到达时间: 您可以扩展:

class myDateTime extends DateTime {
  static $defined_expressions=array(...);

  function __construct($expression=NULL) {
     if ($exp=$this->translate($expression)) {
       parent::__construct($exp); 
     }
  }

  function translate($exp) {
     //check to see if strtotime errors or not
     //if it errors, check if $exp matches a pattern in self::$defined_expressions
     return $exp, modified $exp or false
  }

}

【讨论】:

  • 看起来这个类只是使用了 strtotime() ,它不够充分或不够强大......
  • 它可以同时使用 strtotime 和 ISO8601(使用构造函数)短语。我发现处理自然语言日期时,您几乎必须将语法限制为可定义的集合。您可以考虑通过定义可以映射到标准 PHP 日期表达式的表达式超集来扩展 DateTime。
【解决方案3】:

前段时间我遇到了http://www.timeapi.org,它将自然语言查询转换为时间。虽然它是一个 API。

ruby 源代码在 github 上。如果需要,我想你可以尝试将其移植到 PHP。

【讨论】:

    【解决方案4】:

    刚刚收到来自 PHPClasses 的通知,其中一位获得了月度创新奖的亚军:Text to Timestamp

    你可以试试……

    【讨论】:

    • 看了一下,挺好看的。您甚至可以非常轻松地自行配置和添加其他项目...
    • 查看示例 20 分钟后,即使它执行我想要的操作,我也不知道如何使用它......看起来好像用户应该添加所有可能的查询和映射他们来一次? "$obj->addTimer(time()-60*60*60, time(), '1 小时前');"
    • 是的,我承认,文档看起来很糟糕。尽管如此,我相信已经“安装”了一些默认字符串......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多