Personal License


  • Perpetual license (does not expire)
  • 1 site, unlimited servers
  • No distribution (hosted use only)
  • Commercial use allowed
  • 6 months support
$8.99 Read License

Developer License


  • Perpetual license (does not expire)
  • Unlimited projects
  • Can distribute code and binary products
  • Commercial use allowed
  • 6 months support
$19.99 Read License

14 Day money-back guarantee

Full refund within 14 days of purchase date.

You need to log-in or create an account
  • Create an account
  • Log-in

Please use your real name.

Activation link will be sent to this address.

Minimum 8 characters

Enter your password again

Clicking this button confirms you read and agreed to the terms of use and privacy policy.

  • Released: Jan 6, 2012
    Last Update: Jan 5, 2012
  • Language: PHP
  • Category: Location & Localization
  • Time / costs savings: 10h / $600 *

DateStringParser Class

DateStringParser Class
Developed by Terence Pae, Released Jan 6, 2012

DateStringParser class implements "Natural Language Parsing" which pulls out given date/time from a string. DateStringParser also implements "time ago" feature, pass in given date to return "xx hours ago" or "xx days ago"

PHP

Tags: date , natural language parsing , php , time

DateStringParser class - Natural Language Parsing

Have you ever used Google Calendar, where you just enter sentences, and it parses out the date?

In example, "I have a meeting with Jane tomorrow at 3:00PM" will give you tomorrow's date, along with 3:00PM.

It's called Natural Language Parsing, and with this class, you can easily implement natural language parsing, using just 1 function.

Only a handful of sites currently implement this, Google Calendar being one of them. There's currently no known PHP implementation.

The core of this class is built on PHP's strtotime() function, which is a powerful parsing function built in both PHP4 and PHP5.

It's easy as:

include_once('DateStringParser.inc.php');

$parser = new DateStringParser();

echo $parser->nltotime("tomorrow i am going to grab lunch", "Y-m-d");

which will return tomorrow's date in Y-m-d format.

You can also return filtered string,

echo $parser->nlp_filtered_string();

which will return "i am going to grab lunch"

DateStringParser class - Time Ago

NOTE: This feature can support multiple languages with ease! Check documentation for multi-language support.

Have you ever seen a page where it says, "xxx hours ago" or "xxx minutes ago" ? There's javascript/jquery implementation available, but are you looking for PHP implementation? Have you found them online, and found it impossible to customize?

With this class, you can customize it the way you want it. You can add your own language, change wordings, or even fix granularity.

It's easy as:

echo $parser->time_ago("2011-1-2 03:00:00", 3);

which will return "one year, 4 days, 59 minutes ago"

Also,

echo $parser->time_ago("2011-1-2 03:00:00", 1);

which will return "one year ago"

Check documentation for further details on how to customize this feature.

Back to top

Documentation

Functions:

nltotime($string, $format = NULL) - pass in any form of string, and $format according to PHP Date (http://php.net/manual/en/function.date.php)

nlp_filtered_string() - returns string after nltotime was called, removes parsable words. See example.

time_ago($string, $granularity = 2) - pass in any date format into $string, will automatically parse it. Granularity determines how much information will be displayed.

Examples and Use Cases:

1) Include the class, and create the object

include_once('DateStringParser.inc.php');
$parser = new DateStringParser();

2) Natural Language Parsing

// outputs "2012-01-06"
echo $parser->nltotime("tomorrow i am going to grab lunch", "Y-m-d");

// outputs "i am going to grab lunch"
echo $parser->nlp_filtered_string();

3) Time Ago

// outputs "one year, 4 days, 59 minutes ago"
echo $parser->time_ago("2011-1-2 03:00:00", 3);

// outputs "one year ago"
echo $parser->time_ago("2011-1-2 03:00:00", 1);

Customization and Multi-language Support

lines 56 ~ 88 in the class

private $time_ago = 'ago';

private $time_ago_format = array(
    'seconds' => array(
        'singular' => 'one second',
        'plural' => '%d seconds'
        ),
    'minutes' => array(
        'singular' => 'one minute',
        'plural' => '%d minutes'
        ),
    'hours' => array(
        'singular' => 'one hour',
        'plural' => '%d hours'
        ),
    'days' => array(
        'singular' => 'one day',
        'plural' => '%d days'
        ),
    'weeks' => array(
        'singular' => 'one week',
        'plural' => '%d weeks'
        ),
    'months' => array(
        'singular' => 'one month',
        'plural' => '%d months'
        ),
    'years' => array(
        'singular' => 'one year',
        'plural' => '%d years'
        )
);

The word formatting for time ago can be customized here, depending on how you like it. You can enter different languages, as well as change wordings.

User Reviews

No reviews have been submitted yet.

Questions & Comments


Or enter your name and Email
No comments have been posted yet.
You must be logged-in to vote. Log-in to your account or register now.