Skip to content

Instantly share code, notes, and snippets.

@ewiger
Created March 12, 2013 01:42
Show Gist options
  • Save ewiger/5139596 to your computer and use it in GitHub Desktop.
Save ewiger/5139596 to your computer and use it in GitHub Desktop.
<?php
/**
* FuzzyDateParser is meerly a "convinience" wrapper around date_parse
* functionality from ext/date/lib/timelib code (which does all the
* scanning and other "fuzzy" guessing work).
*
* @see http://www.php.net/manual/en/function.date-parse.php
* @see http://stackoverflow.com/questions/15350309/heuristic-fuzzy-date-extraction-from-the-string
*
* by Yauhen Yakimovich, 2013
*
* Usage example:
*
* <code>
* echo FuzzyDateParser::fromText('banana 1/2/3');
* echo FuzzyDateParser::fromText('Joe Soap was born on 12 February 1981'));
* echo FuzzyDateParser::fromText('2005 Feb., reprint'));
* echo FuzzyDateParser::fromText('!'); # will fail to parse, producing an empty string.
* echo FuzzyDateParser::fromText('monkey 2010-07-10 loves bananas and php');
* </code>
*
*/
class FuzzyDateParser
{
public $verbose = false;
public $result;
public $hasFailed = false;
public static function fromText($text)
{
$instance = new FuzzyDateParser();
return $instance->parse($text);
}
public function parse($text)
{
static $ignoredErrors = array(
'The timezone could not be found in the database',
);
$this->result = date_parse($text);
$errors = array();
if ($this->result['error_count'] > 0) {
foreach ($this->result['errors'] as $errorMessage) {
if (!in_array($errorMessage, $ignoredErrors)) {
$errors[] = $errorMessage;
}
}
}
$this->result['error_count'] = count($errors);
$this->result['errors'] = $errors;
$this->hasFailed = ($this->result['error_count'] > 0
&& $this->result['year'] === false
&& $this->result['year'] === false
&& $this->result['day'] === false);
if ($this->hasFailed and $this->verbose) {
print_r($this->result);
}
return $this;
}
function __toString()
{
return ($this->hasFailed) ? '' : sprintf(
'%04d-%02d-%02d',
$this->result['year'],
$this->result['month'],
$this->result['day']
);
}
}
@inventnyc
Copy link

This is fantastic and much more eloquent than my attempt! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment