0

I am trying to create a function that will return text that indicates date. For example, "today" is the text in following sentence which indicates date.

"I want to meet with you today".

Following functions serve the task except for few words like "I can" "u can" etc. Can anyone tell me why it returns "I can" as date text? any solution?

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);
}
T.ra
  • 11
  • 3
  • Can you post an example of how this function would be called? Otherwise we have to just guess. – Jonathan Jul 06 '18 at 17:35
  • Here is how this function will be called- date_text("Yes I can meet with you"), 0, 0) or date_text("I want to meet with you today"), 0, 0). It works fine for second one and return "today" but it also return "I can" as date text for first one which should not be. – T.ra Jul 06 '18 at 17:38
  • but what are `$text`, `$offset` and `$length` other than random variables to me. – Jonathan Jul 06 '18 at 17:40
  • And a quick look at the manual for date formats shows that `I` is treated as the roman numeral `1`. A quick test by me shows that that followed by almost any string around 5/6 characters long (or less) will return now essentially (maybe some sort of time offset. http://php.net/manual/en/datetime.formats.date.php – Jonathan Jul 06 '18 at 17:42
  • A lot of these strings like this can be interpreted to be some form of timezone. For example `U` likely refers to `UTC`. This sort of parsing would probably be near impossible like you are doing. I would suggest looking for set keywords first (today, day of week, etc) and testing those. You would need to come up with any number of possible combinations. – Jonathan Jul 06 '18 at 17:51

1 Answers1

0

strtotime is detecting the 'I' and assuming that it's a military timezone (India Time Zone).

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

brogrammer
  • 11
  • 1