Note that there are some explanatory texts on larger screens.

plurals
  1. POEnglish to Time
    text
    copied!<p>Does anyone know of a good class / library to convert English representations of time into timestamps?</p> <p>The goal is to convert natural language phrases such as "ten years from now" and "three weeks" and "in 10 minutes" and working out a best match unix timestamp for them.</p> <p>I have hacked up some pretty poor and untested code to get going on it, but I am sure there are great parsers out there for calendars and such.</p> <pre><code>private function timeparse($timestring) { $candidate = @strtotime($timestring); if ($candidate &gt; 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) &lt; 5) // 10th || 2nd (or probably a number) { $day = preg_replace("/\D/", "", $timestring); if ($day &gt; 0) { $month = date("m"); $year = date("y"); return strtotime("$month/$day/$year"); } else { return false; } } return false; // No can do. } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload