Wednesday 15 April 2015

php - English to Time -


Is there a good class / library address to represent English in timestamp?

The goal is to convert natural language phrases such as "ten years from now" and "three weeks" and "in 10 minutes" and the best match for them is to do Unix timestamps.

I go to some pretty poor and untouched code, but I'm sure there are very good parsers for the calendar and in such a way.

  on private function tile ($ timestard) {$ candidate = @strtotime ($ TIMESTRING); If ($ candidate & gt; time ()) will return the $ candidate; // let's have a bash on php // $ thisyear = date ("y"); If (Strip ($ timestring, "minute")! == incorrect) // reference minute is {$ nummins = preg_replace ("/ \ D /", "", $ timeline); $ Candidate = @trotoime ("now + $ nummins min"); $ Return; } If (SRPO ($ timestring, "yes")! == incorrect) // reference hour is {$ numhours = preg_replace ("/ \ D /", "", $ timeline); $ Candidate = @trotoime ("now + $ numhours hour"); $ Return; } If (strips ($ timestring, "day")! == incorrect) // is reference day {$ numdays = preg_replace ("/ \ D /", "", $ timestard); $ Candidate = @strtotime ("now + $ numdays days"); $ Return; } If (strips ($ timestring, "year")! == incorrect) // reference year (2 years) {$ numyears = preg_replace ("/ \ D /", "", $ timestring); $ Candidate = @strtotime ("now + $ numyears years"); $ Return; } If (strangel ($ timestring) <5) // 10th || 2 (or maybe a number) {$ day = preg_replace ("/ \ D /", "", $ timestard); If ($ day> 0) {$ month = date ("m"); $ Year = date ("y"); Return Stratetime ("$ Month / $ Day / $ Year"); } Other {return false; }     }      return false; // no one could. }  

Use the square.

Example:

$ string = 'Four days ago'; $ D = date_create ($ string); $ D & gt; GetTimestamp ();

ETA: which you can extend:

  class myDateTime provides the date of the date {static $ defined_expressions = array (...); Function __ conversion ($ expression = null) {if ($ exp = $ this-> translate ($ expression)) {Original: __ Construction ($ EXP); }} Function translate ($ exp) {// Check if strtotime errors to see or not // If these are errors, check that the $ exp matches a pattern in itself: $ defined_expression $ exp, modified $ Exp or false}}  

No comments:

Post a Comment