DateStringParser Class

DateStringParser Class

Released 6 years ago , Last update 6 years ago

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"

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:


$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"


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.

  • 5 1
  • 4 0
  • 3 0
  • 2 0
  • 1 0
1 Reviews Read Reviews


14 day 14-day money-back guarantee


Personal License

  • Perpetual license

  • 1 site, unlimited servers

  • No distribution (hosted use only)

  • Commercial use

  • 6 months support



nltotime($string, $format = NULL) - pass in any form of string, and $format according to PHP Date (

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

$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.

2 licenses, starting from From » $8.99 View Licenses

Get A Quote

What do you need?
  • Custom development
  • Integration
  • Customization / Reskinning
  • Consultation
When do you need it?
  • Soon
  • Next week
  • Next month
  • Anytime

Thanks for getting in touch!

Your quote details have been received and we'll get back to you soon.

Or enter your name and Email
No comments have been posted yet.