Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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_Time_Timestamp - Fatal编程技术网

Php 英语时间

Php 英语时间,php,time,timestamp,Php,Time,Timestamp,有没有人知道一个很好的类/库可以将时间的英文表示转换为时间戳 目标是转换自然语言短语,如“十年后”、“三周”和“十分钟内”,并为它们计算出最佳匹配的unix时间戳 我已经破解了一些非常糟糕的未经测试的代码,但我确信有很多优秀的日历解析器 private function timeparse($timestring) { $candidate = @strtotime($timestring); if ($candidate > time()) return $candida

有没有人知道一个很好的类/库可以将时间的英文表示转换为时间戳

目标是转换自然语言短语,如“十年后”、“三周”和“十分钟内”,并为它们计算出最佳匹配的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.
}
私有函数timeparse($timestring)
{
$candidate=@strottime($timestring);
if($candidate>time())返回$candidate;//让php试试看
//$thisyear=日期(“Y”);
if(strpos($timestring,“min”)!==false)//上下文为分钟
{
$nummins=preg\u replace(“/\D/”,“”,$timestring);
$candidate=@strotime(“现在+$numins分钟”);
返回$candidate;
}
if(strpos($timestring,“hou”)!==false)//上下文为小时
{
$numhours=preg_replace(“/\D/”,“”,$timestring);
$candidate=@strotime(“现在+numhours小时”);
返回$candidate;
}
if(strpos($timestring,“day”)!==false)//上下文是days
{
$numdays=preg_replace(“/\D/”,“”,$timestring);
$candidate=@strotime(“现在+numdays天”);
返回$candidate;
}
if(strpos($timestring,“year”)!==false)//上下文是年(2年)
{
$numyears=preg\u replace(“/\D/”,“”,$timestring);
$candidate=@strotime(“现在+numyears年”);
返回$candidate;
}
if(strlen($timestring)<5)//10 | | 2(或可能是一个数字)
{
$day=preg_replace(“/\D/”、“”、$timestring);
如果($day>0)
{
$month=日期(“m”);
$year=日期(“y”);
返回标准时间($month/$day/$year);
}
其他的
{
返回false;
}
}
return false;//不可以。
}
和的
NSDateFormatter
能够处理此类时间表示。GNUStep版本是开源的。

使用该类

e、 g:

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

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
  }

}

不久前,我遇到了一种将自然语言查询转换为时间的方法。不过这是一个API


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

刚刚收到PHPClasses的通知,其中一位是月度创新奖的亚军:


您可以尝试…

考虑到php标记,这不太可能有多大帮助。我认为mouviciel想表达的观点是,他可以将库移植到php。谢谢你发布这篇文章。我很欣赏这个建议,但进入那里有很大的障碍,我不知道效果如何,因此我不愿意投入时间。如果我找不到另一个解决方案,我会研究它。看起来这个类只使用strotime(),它不够充分或功能不够强大……它可以同时使用strotime和ISO8601(使用构造函数)短语。我发现,在处理自然语言日期时,几乎必须将语法限制在可定义的集合内。您可以考虑通过定义一个表达式的超集来扩展DATETIME,您可以将其映射到标准PHP日期表达式。“三年,两天,五分钟以后”作为输入如何?是的,确实是这样的。这就是我所需要的。“三年两天五分钟”和类似的应该很容易转化为ISO8601间隔:“P3Y2DT5M”,您可以将其馈送到DateInterval并添加到DateTime对象。正则表达式无法解析这些。您需要的是一个语法解析器(bison等),这在技术上就像创建一个迷你编译器:这是一个有趣的挑战。在我看来,如果有一组用户提交的日期数据能够让你感觉到你的用户群是如何表达这样的日期的,那就太好了。你甚至可以很容易地配置它并添加其他项目…看了20分钟的示例后,我不知道如何使用它,即使它符合我的要求。。。看起来似乎用户需要添加所有可能的查询并将它们映射到一个时间?“$obj->addTimer(time()-60*60*60,time(),‘1小时前’);”是的,我承认,文档看起来很糟糕。不过,我相信已经“安装”了一些默认字符串。。。
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
  }

}