英语到时间

Ant*_*thy 8 php time timestamp

有没有人知道一个好的类/库将时间的英文表示转换成时间戳?

目标是转换自然语言短语,例如"从现在开始十年"和"三周"以及"在十分钟内",并为他们制定最佳匹配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.
}
Run Code Online (Sandbox Code Playgroud)

mou*_*iel 0

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