【问题标题】:why strtotime("I can") return date value in php?为什么 strtotime("I can") 在 php 中返回日期值?
【发布时间】:2018-07-06 17:27:41
【问题描述】:

我正在尝试创建一个函数,该函数将返回指示日期的文本。例如,“今天”是以下句子中表示日期的文本。

“我今天想见你”。

以下函数用于完成任务,除了“我可以”“你可以”等几个词。谁能告诉我为什么它返回“我可以”作为日期文本?有什么解决办法吗?

public function date_text($text, $offset, $length){

    $parseArray = preg_split( "/[\s,.]/", $text);
    $dateTest = implode(" ", array_slice($parseArray, $offset, $length == 0 ? null : $length));

        $date = strtotime($dateTest);

    if ($date){
         return $dateTest;
    }

    //make the string one word shorter in the front
    $offset++;

    //have we reached the end of the array?
    if($offset > count($parseArray)){

        //reset the start of the string
        $offset = 0;

        //trim the end by one
        $length--;

        //reached the very bottom with no date found
        if(abs($length) >= count($parseArray)){
            return false;
        }
    }

    //try to find the date with the new substring
    return $this->date_text($text, $offset, $length);
}

【问题讨论】:

  • 你能发布一个如何调用这个函数的例子吗?否则我们只能猜测。
  • 这个函数的调用方式如下 - date_text("Yes I can meet you"), 0, 0) or date_text("I want to meet with you today"), 0, 0 )。它适用于第二个并返回“今天”,但它也返回“我可以”作为第一个不应该的日期文本。
  • 但对我来说,除了随机变量之外,$text$offset$length 是什么。
  • 快速查看日期格式手册会发现I 被视为罗马数字1。我的一个快速测试表明,几乎所有大约 5/6 个字符长(或更短)的字符串现在基本上都会返回(可能是某种时间偏移。php.net/manual/en/datetime.formats.date.php
  • 很多这样的字符串可以解释为某种形式的时区。例如U 可能指的是UTC。像您所做的那样,这种解析可能几乎是不可能的。我建议首先寻找设置的关键字(今天、星期几等)并测试它们。您需要想出任意数量的可能组合。

标签: php date text strtotime preg-split


【解决方案1】:

strtotime 正在检测“I”并假设它是军事时区(印度时区)。

https://www.timeanddate.com/time/zones/military

【讨论】:

    猜你喜欢
    • 2013-06-09
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多