qualcuno sa di una buona classe/biblioteca per convertire le rappresentazioni in inglese di tempo in timestamp?Inglese a Tempo
L'obiettivo è quello di convertire frasi in linguaggio naturale come "dieci anni da oggi" e "tre settimane" e "in 10 minuti" e di elaborare una migliore corrispondenza unix timestamp per loro.
ho violato un po 'di codice piuttosto scarsa e non testato per andare avanti su di esso, ma sono sicuro che ci sono grandi parser là fuori per i calendari e così via.
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.
}
Come su "tre anni, due giorni e cinque minuti nel futuro "come input? – chelmertz
Sì, esattamente cose del genere. Questo è quello di cui ho bisogno. –
"tre anni, due giorni e cinque minuti" e simili dovrebbe tradurre facilmente in un intervallo ISO8601: "P3Y2DT5M", che si può nutrire per un DateInterval e aggiungere ad un oggetto DateTime. – dnagirl