Personal License $8.99

1 site, unlimited servers No source distribution Commercial use allowed Can modify source 6 months support Read full license

Developer License $19.99

Unlimited projects Source and binary distribution Commercial use allowed Distribute modifications 6 months support Read full license

You need to log-in or create an account
  • Create an account
  • Log-in
  • Please use your real name.
  • Account activation link will be sent to this address.
  • Minimum 8 characters

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

Starting from $ 8.99

View Pricing 14 days money-back guarantee

DateStringParser Class

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"

Description

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.

Questions & Comments

No questions have been posted yet

Leave a comment

You must be logged-in to leave a comment.
Log-in now or register for a free account.
You must be logged-in to vote. Log-in to your account or register now.

User Reviews

No reviews have been submitted yet.

Starting from $ 8.99

View Pricing 14 days money-back guarantee